gpt4 book ai didi

javascript - Karma 代码覆盖率在测试中始终显示 100% (0/0)

转载 作者:行者123 更新时间:2023-11-28 05:11:14 31 4
gpt4 key购买 nike

我正在尝试构建一个 react 样板,以便我可以将它用于我的个人项目。现在,我正在尝试集成代码覆盖率功能。我希望所有测试文件都驻留在其各自的组件文件夹中,而不是创建单独的测试文件夹。以下是存储库的链接。

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

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!
},
});
};

测试.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);

我尝试解决与我的问题类似的各种问题,但没有一个真正解决我的问题。他们中的大多数都以不同的方式进行配置。我不确定我做错了什么。我尝试了各种技术,但所有努力都是徒劳的。测试似乎工作正常,但覆盖率始终显示 100%。

最佳答案

我也有类似的问题。我发现 zone.js 元素的导入需要遵循严格的流程。重要的一项是代理,它需要放置在不同测试类型导入之后。

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');

关于javascript - Karma 代码覆盖率在测试中始终显示 100% (0/0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41366155/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com