handler.ts:
报道:
-----------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------------|---------|----------|---------|---------|-------------------
All files | 97.05 | 100 | 75 | 96.96 |
src | 96.87 | 100 | 66.66 | 96.77 |
handler.ts | 96.87 | 100 | 66.66 | 96.77 | 90
jest.config.js:
module.exports = {
clearMocks: true,
transform: {
'.(js|ts)$': ['esbuild-jest', { sourcemap: true }],
},
setupFilesAfterEnv: ['./jest.setup.ts'],
collectCoverage: true,
};
我打赌转换是问题所在,只是不知道如何解决它。
试过:
/* istanbul ignore next */
/* istanbul ignore next */
之后添加export
/* istanbul ignore next */
之后添加return
当我这样做时,我仍然得到一条未覆盖的线。所有的中间件等使得实际测试该功能不值得付出努力。
添加解析提示评论忽略函数的正确方法,但esbuildonly在构建输出中包含“法律评论”(强调我的):
“法律评论”被认为是任何声明级别的评论 JS 或 CSS 中的规则级注释 包含
或@license
或以@preserve
或//!
开头的。这些评论是 默认情况下保留在输出文件中,因为这遵循了 代码的原始作者。/*!
所以给定一个实际的 MRE:
// index.js
export default function foo() {
console.log("Can't touch this");
}
// index.test.js
import func from "./index";
it("doesn't test anything", () => {
expect(true).toBe(true);
});
// jest.config.js
module.exports = {
transform: {
"\\.js$": ["esbuild-jest", { sourcemap: true }],
},
};
// package.json
{
"devDependencies": {
"esbuild": "^0.17.14",
"esbuild-jest": "^0.5.0",
"jest": "^29.5.0"
},
"scripts": {
"test": "jest"
}
}
输出:
$ npm t -- --coverage
> test
> jest --coverage
PASS ./index.test.js
✓ doesn't test anything (1 ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 50 | 100 | 0 | 50 |
index.js | 50 | 100 | 0 | 50 | 2
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.16 s, estimated 1 s
Ran all test suites.
您可以通过添加包含 @preserve
// index.js
/* istanbul ignore next @preserve */
export default function foo() {
console.log("Can't touch this");
}
新输出:
$ npm t -- --coverage
> test
> jest --coverage
PASS ./index.test.js
✓ doesn't test anything (1 ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 0 | 100 |
index.js | 100 | 100 | 0 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.363 s, estimated 1 s
Ran all test suites.