简体   繁体   English

Karma代码覆盖率始终显示为测试的100%(0/0)

[英]Karma code coverage always shows 100% (0/0) for the tests

I am trying to build a react boilerplate so that I can use it for my personal project. 我正在尝试构建一个反应样板,以便我可以将它用于我的个人项目。 Right now, I am trying to integrate the code coverage feature. 现在,我正在尝试集成代码覆盖功能。 I want all the test files to reside in its respective component folder rather than creating a separate tests folder. 我希望所有测试文件都驻留在其各自的组件文件夹中,而不是创建单独的测试文件夹。 Below is the link to the repo. 下面是回购的链接。

https://github.com/shettyrahul8june/react-webpack-beej https://github.com/shettyrahul8june/react-webpack-beej

karma.conf.js karma.conf.js

import webpackConfig from './webpack.config';

webpackConfig.devtool = 'inline-source-map';

module.exports = (config) => {
  config.set({
    browsers: ['Chrome'], // run in Chrome
    singleRun: true, // just run once by default
    colors: true,
    // autoWatch: true,
    // logLevel: config.LOG_DEBUG,
    // use the mocha, chai and sinon test framework
    frameworks: ['mocha', 'chai', 'sinon'],
    files: [
      'tests.webpack.js', // just load this file
    ],
    preprocessors: {
      // preprocess with webpack and our sourcemap loader
      'tests.webpack.js': ['webpack', 'sourcemap'],
    },
    plugins: [
      'karma-chrome-launcher',
      'karma-chai',
      'karma-mocha',
      'karma-sinon',
      'karma-sourcemap-loader',
      'karma-webpack',
      'karma-coverage',
    ],
    // report results in this format
    reporters: ['progress', 'coverage'],
    coverageReporter: {
      dir: '../coverage',
      reporters: [
        { type: 'text-summary' },
        { type: 'html' },
        { type: 'lcov' },
      ],
    },
    webpack: {
      node: {
        fs: 'empty',
      },
      module: webpackConfig.module,
      resolve: webpackConfig.resolve,
      externals: Object.assign({}, webpackConfig.externals, {
        'react/addons': true,
        'react/lib/ExecutionEnvironment': true,
        'react/lib/ReactContext': 'window',
      }),
    },
    webpackServer: {
      noInfo: true, // please don't spam the console when running in karma!
    },
  });
};

test.webpack.js test.webpack.js

// make sure you have your directory and regex test set correctly!
const context = require.context('../src/', true, /.+\.test\.jsx?$/);
context.keys().forEach(context);

I tried going through various questions which are similar to mine but none actually solve my problem. 我尝试了各种类似于我的问题,但没有一个能解决我的问题。 Most of them have configured in a different way. 他们中的大多数都以不同的方式配置。 I am not sure what am I doing wrong. 我不确定我做错了什么。 I tried various techniques but all efforts where in vain. 我尝试了各种各样的技术,但所有努力都徒劳无功。 Test seems to work fine but the coverage always shows 100%. 测试似乎工作正常,但覆盖率总是显示100%。

I had a similar issue. 我有一个类似的问题。 I found that the import of the zone.js elements needed to follow a strict flow. 我发现zone.js元素的导入需要遵循严格的流程。 The important item was proxy, it needed to be placed after the different test type imports. 重要的项目是代理,它需要在不同的测试类型导入之后放置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM