Karma覆盖范围始终为0/0

时间:2017-10-13 15:40:23

标签: typescript webpack jasmine karma-webpack

大家好,我是网络新手,有时它会像魔术那样找我,但我真的不明白它是如何运作的。

所以我有1个spec.ts文件,包含3个测试。测试运作良好:

HeadlessChrome 0.0.0 (Windows 7 0.0.0): Executed 3 of 3 SUCCESS (0.255
secs / 0.027 secs)

但是报道总是显示0/0:

=============================== Coverage summary ===============================
Statements   : 100% ( 0/0 )
Branches     : 100% ( 0/0 )
Functions    : 100% ( 0/0 )
Lines        : 100% ( 0/0 )
================================================================================

我查看过很多例子,但我不明白它有什么问题。

这是我的 karma.config.js

module.exports = function (config) {
    config.set({
        basePath: ".",

        frameworks: ["jasmine"],

        files: [
            "./wwwroot/dist/vendor.js",
            "./boot-tests.js"
        ],

        preprocessors: {
            "./boot-tests.js": ["webpack", "sourcemap"],
            "./ClientApp/app/**/!(spec).ts": ["coverage"]
        },

        coverageReporter: {
            includeAllSources: true,
            reporters: [
                { type: "in-memory" }
            ]
        },

        remapCoverageReporter: {
            "text-summary": null,
            html: "./_report/coverage/html-report"
        },

        reporters: [
            "html",
            "coverage",
            "remap-coverage",
            "progress"
        ],

        htmlReporter: {
            outputFile: "index.html",
            outputDir: "_report/tests"
        },

        webpackServer: {
            noInfo: true
        },

        webpack: require("./webpack.config.js")()
            .filter(conf =>
                conf.target !== "node"
            ),

        webpackMiddleware: {
            stats: "errors-only"
        },

        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: false,
        browsers: ["ChromeHeadless"],
        singleRun: true
    });
};

引导tests.js

Error.stackTraceLimit = Infinity;

require("core-js");
require("core-js/es6");
require("core-js/es7/reflect");

require("zone.js/dist/zone");
require("zone.js/dist/long-stack-trace-zone");
require("zone.js/dist/proxy");
require("zone.js/dist/sync-test");
require("zone.js/dist/async-test");
require("zone.js/dist/fake-async-test");
require("zone.js/dist/jasmine-patch");

require("rxjs/Rx");

var testing = require("@angular/core/testing");
var browser = require("@angular/platform-browser-dynamic/testing");

testing.TestBed.initTestEnvironment(
  browser.BrowserDynamicTestingModule,
  browser.platformBrowserDynamicTesting()
);

Object.assign(global, testing);

var appContext = require.context("./ClientApp/app", true, /\.spec\.ts/);
appContext.keys().map(appContext);

webpack.config.js

const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;

module.exports = (env) => {
    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { modules: false },
        context: __dirname,
        resolve: { extensions: ['.js', '.ts'] },
        output: {
            filename: '[name].js',
            publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
        },
        module: {
            rules: [
                { test: /\.ts$/, exclude: /node_modules/, include: /ClientApp/, use: ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] },
                { test: /\.html$/, exclude: /node_modules/, use: 'html-loader?minimize=false' },
                { test: /\.css$/, exclude: /node_modules/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize'] },
                { test: /\.scss$/, exclude: /node_modules/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize', 'sass-loader'] },            
                { test: /\.(png|jpg|jpeg|gif|svg)$/, exclude: /node_modules/, use: 'url-loader?limit=25000' }
            ]
        },
        plugins: [new CheckerPlugin()]
    };

    // Configuration for client-side bundle suitable for running in browsers
    const clientBundleOutputDir = './wwwroot/dist';
    const clientBundleConfig = merge(sharedConfig, {
        entry: { 'main-client': './ClientApp/boot-client.ts' },
        output: { path: path.join(__dirname, clientBundleOutputDir) },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./wwwroot/dist/vendor-manifest.json')
            })
        ].concat(isDevBuild ? [
            // Plugins that apply in development builds only
            new webpack.SourceMapDevToolPlugin({
                filename: '[file].map', // Remove this line if you prefer inline source maps
                moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
        ] : [
                // Plugins that apply in production builds only
                new webpack.optimize.UglifyJsPlugin()
            ])
    });

    return [clientBundleConfig];
};

package.json

{
  "name": "myapp",
  "version": "0.0.1",
  "scripts": {
    "test": "karma start ./karma.conf.js"
  },
  "dependencies": {
    "@angular/animations": "^4.4.3",
    "@angular/common": "^4.4.3",
    "@angular/compiler": "^4.4.3",
    "@angular/core": "^4.4.3",
    "@angular/forms": "^4.4.3",
    "@angular/http": "^4.4.3",
    "@angular/platform-browser": "^4.4.3",
    "@angular/platform-browser-dynamic": "^4.4.3",
    "@angular/platform-server": "^4.4.3",
    "@angular/router": "^4.4.3",
    "angular2-template-loader": "^0.6.2",
    "aspnet-prerendering": "^2.0.5",
    "aspnet-webpack": "^1.0.29",
    "awesome-typescript-loader": "3.2.3",
    "bootstrap": "3.3.7",
    "crypto-js": "3.1.9-1",
    "css": "2.2.1",
    "css-loader": "0.28.5",
    "es6-shim": "0.35.3",
    "event-source-polyfill": "0.0.9",
    "expose-loader": "0.7.3",
    "extract-text-webpack-plugin": "3.0.1",
    "file-loader": "0.11.2",
    "html-loader": "0.5.1",
    "isomorphic-fetch": "2.2.1",
    "jquery": "3.2.1",
    "json-loader": "0.5.7",
    "ng2-file-upload": "1.2.1",
    "ngx-bootstrap": "1.9.1",
    "ngx-cookie-service": "1.0.7",
    "parse5": "3.0.2",
    "preboot": "^5.0.0",
    "reflect-metadata": "0.1.10",
    "rxjs": "^5.4.3",
    "style-loader": "0.18.2",
    "to-string-loader": "1.1.5",
    "ts-loader": "2.3.3",
    "typescript": "^2.5.2",
    "url-loader": "0.5.9",
    "webpack": "2.5.1",
    "webpack-externals-plugin": "1.0.0",
    "webpack-hot-middleware": "2.18.2",
    "webpack-merge": "4.1.0",
    "zone.js": "0.8.17"
  },
  "devDependencies": {
    "@types/jasmine": "2.5.47",
    "@types/node": "^7.0.32",
    "jasmine-core": "2.6.4",
    "karma": "1.7.0",
    "karma-chrome-launcher": "2.2.0",
    "karma-html-reporter": "^0.2.7",
    "karma-coverage": "1.1.1",
    "karma-cli": "1.0.1",
    "karma-jasmine": "1.1.0",
    "karma-webpack": "2.0.3",
    "karma-remap-coverage": "0.1.4",
    "karma-sourcemap-loader": "0.3.7",
    "node-sass": "^4.5.3",
    "raw-loader": "^0.5.1",
    "sass-loader": "^6.0.6"
  }
}

如果你能告诉我在哪里犯了错误,我将不胜感激。

0 个答案:

没有答案