4

如何按单独的文件拆分覆盖范围?现在,捆绑文件一切正常。我正在使用Angular,Webpack进行捆绑和Karma/ Mocha/Chai进行测试。

我有以下 webpack.config:

module.exports = {
    entry: {
        public: "./application/src/public.js",
        office: "./application/src/office.js"
    },
    output: {
        path: "./client/javascripts/",
        filename: "[name].js",
        sourceMapFilename: "[name].js.map"
    }
};

并遵循 karma.conf.js:

module.exports = function(config) {
config.set({
    basePath: "",
    frameworks: ["mocha", "chai"],
    reporters: ["mocha", "coverage"],
    files: [
        "./client/libs/angular/angular.js",
        "./client/libs/angular-route/angular-route.js",
        "./client/libs/angular-animate/angular-animate.js",
        "./client/libs/angular-cookies/angular-cookies.js",
        "./client/libs/angular-mocks/angular-mocks.js",
        "./client/libs/angular-messages/angular-messages.js",
        "./client/javascripts/office.js", /* the bundle */
        "./application/**/*_tests.js"
    ],
    preprocessors: {
        "./client/javascripts/office.js": ["coverage"]
    },
    coverageReporter: {
        type: "html",
        dir: "coverage/"
    },
    exclude: [],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ["PhantomJS"],
    singleRun: false
});
};

样本测试文件:

describe("AccountLoginCtrl", function() {
        beforeEach(angular.mock.module("stAccount"));

        var $controller;
        var AccountLoginCtrl;
        var $scope;

        beforeEach(angular.mock.inject(function(_$controller_) {
            $controller = _$controller_;
            $scope = {};
            AccountLoginCtrl = $controller("AccountLoginCtrl", {
                $scope: $scope
            });
        }));

        it("should exist", function() {
            expect(AccountLoginCtrl).to.exist;
        });
    });
4

1 回答 1

2

设置文件夹结构:

karma.conf.js
./application
    ./src
        ./components
        entry.js
    ./tests
        index.js
        ./components
    ./coverage

添加文件夹作为测试的入口点index.jstests

// adds all test files to the bundle.
var testsContext = require.context("./components", true, /\.js$/);
testsContext.keys().forEach(testsContext);

// adds the application to the bundle.
var componentsContext = require("../src/entry"); 

业力.conf.js

var path = require("path");
module.exports = function(config) {
    config.set({
        basePath: "",
        frameworks: ["mocha"],
        reporters: ["mocha", "coverage"],
        files: [
            "./client/libs/angular/angular.js",
            "./client/libs/angular-route/angular-route.js",
            "./client/libs/angular-animate/angular-animate.js",
            "./client/libs/angular-cookies/angular-cookies.js",
            "./client/libs/angular-mocks/angular-mocks.js",
            "./client/libs/angular-messages/angular-messages.js",
            "./application/tests/index.js",
        ],
        preprocessors: {
            "./application/tests/index.js": ["webpack"]
        },
        webpack: {
            module: {
                preLoaders: [
                    {
                        test: /\.js$/,
                        include: path.resolve("./application/src/"),
                        loader: "isparta"
                    }
                ]
            }

        },
        webpackMiddleware: {
            noInfo: true
        },
        coverageReporter: {
            type: "html",
            dir: "./application/coverage/"
        },
        exclude: [],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ["PhantomJS"],
        singleRun: false
    });
};

包.json

{
"devDependencies": {
    "chai": "^3.4.0", 
    "isparta-loader": "^1.0.0",
    "karma": "^0.13.12",
    "karma-coverage": "^0.5.3",
    "karma-mocha": "^0.2.0",
    "karma-mocha-reporter": "^1.1.1",
    "karma-phantomjs-launcher": "^0.2.1",
    "karma-webpack": "^1.7.0",
    "mocha": "^2.3.3",
    "phantomjs": "^1.9.18",
    "webpack": "^1.12.2"
  }
}
于 2015-10-22T13:27:48.707 回答