gpt4 book ai didi

javascript - 为什么更改 testMatch 正则表达式时 Jest 覆盖率报告会中断?

转载 作者:行者123 更新时间:2023-12-03 00:49:03 25 4
gpt4 key购买 nike

使用以下配置,更改 testMatch cli 选项时,覆盖率报告会中断。我尝试设置 collectCoverageFrom './src/*.js' 选项,但这也没有解决问题。

./src/sum.js

function sum(a, b) {
return a + b;
}
module.exports = sum;

./test/sum.js

const sum = require('../src/sum');

test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});

package.json

"devDependencies": {
"@babel/core": "^7.1.2",
"@babel/plugin-proposal-decorators": "^7.1.2",
"@babel/preset-env": "^7.1.0",
"@babel/register": "^7.0.0",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"jest": "^23.6.0"
},

"scripts": {
"test": "jest ./test/*.js --testMatch '**/*.js' --coverage --coverageDirectory './test-coverage'"
}

输出来自npm run test

 PASS  test/sum.js
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | Unknown | Unknown | Unknown | Unknown | |
----------|----------|----------|----------|----------|-------------------|

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.125s
Ran all test suites matching /.\/test\/sum.js/i.

最佳答案

设置testRegex参数:

jest --testRegex='./test/.*\.js$' --coverage --coverageDirectory './test-coverage'

现在输出如下所示:

PASS  test/sum.js
✓ adds 1 + 2 to equal 3 (3ms)

----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
sum.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.769s, estimated 1s
Ran all test suites.

关于javascript - 为什么更改 testMatch 正则表达式时 Jest 覆盖率报告会中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53133359/

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