gpt4 book ai didi

javascript - 为什么我的 Karma Jasmine 代码覆盖率总是 100%

转载 作者:行者123 更新时间:2023-11-30 16:56:42 25 4
gpt4 key购买 nike

我正在使用 Grunt 通过 Karma/Jasmine 运行我的单元测试,我正在使用 Karma 进行代码覆盖,但是,代码覆盖率总是 100%,因为它似乎找不到文件,这里是一些输出:

PhantomJS 1.9.8 (Mac OS X) 信息:'开始测试...'

PhantomJS 1.9.8 (Mac OS X): Executed 2 of 2 SUCCESS (0.003 secs / 0.021 secs)
DEBUG [karma]: Run complete, exiting.
DEBUG [launcher]: Disconnecting all browsers
DEBUG [proxy]: failed to proxy /app/view/SearchForm.js?_dc=1428867938629 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/view/Main.js?_dc=1428867938628 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/view/ImageDetailView.js?_dc=1428867938629 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/view/ItemDetailView.js?_dc=1428867938629 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/controller/News.js?_dc=1428867938630 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/controller/Home.js?_dc=1428867938630 (browser hung up the socket)
DEBUG [coverage]: Writing coverage to /appdir/coverage/PhantomJS 1.9.8 (Mac OS X)
----------|-----------|-----------|-----------|-----------|
File | % Stmts |% Branches | % Funcs | % Lines |
----------|-----------|-----------|-----------|-----------|
----------|-----------|-----------|-----------|-----------|
All files | 100 | 100 | 100 | 100 |
----------|-----------|-----------|-----------|-----------|

DEBUG [launcher]: Process PhantomJS exited with code 0
DEBUG [temp-dir]: Cleaning temp dir

Done, without errors.

如您所见,单元测试可以找到要测试的正确文件,但代码覆盖率插件找不到相同的文件。我认为这是因为我使用的是 grunt-contrib-connect 而不是内置的 Karma Web 服务器。当我使用内置的 karma 服务器时,单元测试也不起作用,因为它们也找不到文件。这是我的 karma.conf:

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

frameworks: [ 'jasmine' ],

files: [
'touch/sencha-touch-all.js',
'setup.js',
'app.js',
{
pattern: 'app/**/*.js',
watched: true,
included: false,
served: true
},
'tests/**/*.js'
],

proxies: {
'/': 'http://localhost:9000/'
},

preprocessors: {
'app/**/*.js': ['coverage']
},
reporters: ['junit', 'progress', 'coverage'],

coverageReporter: {
type: 'text'
},

port: 9876,
colors: true,
logLevel: config.LOG_DEBUG,
autoWatch: true,
browsers: [ 'PhantomJS' ],
captureTimeout: 60000,
singleRun: false
});
};

我认为 grunt-contrib-connect 服务器在代码覆盖开始之前就退出了。有谁知道为什么或如何阻止它?更好的是,有谁知道为什么内置的 karma 服务器不起作用。以下是我使用内置 karma 服务器时发生的情况:

DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)

等....

最佳答案

您的问题可能是由于您的文件设置没有提供覆盖率插件期望的文件,如 included: false设置需要从脚本加载器(如 require.js)手动加载。

    files: [
'touch/sencha-touch-all.js',
'setup.js',
'app.js',
{
pattern: 'app/**/*.js',
watched: true,
included: false,
served: true
},
'tests/**/*.js'
],

尝试不使用手动加载程序的文件设置,让 karma 加载文件以查看是否适合您。

    files: [
'touch/sencha-touch-all.js',
'setup.js',
'app.js',
'app/**/*.js',
'tests/**/*.js'
],

关于javascript - 为什么我的 Karma Jasmine 代码覆盖率总是 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29594000/

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