如何忽略覆盖函数?

问题描述 投票:0回答:1

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

我打赌转换是问题所在,只是不知道如何解决它。

试过:

  • 在第 88 行添加
    /* istanbul ignore next */
  • 在第 89 行的
     /* istanbul ignore next */ 
    之后添加
    export
  • 在第 90 行的
     /* istanbul ignore next */ 
    之后添加
    return

当我这样做时,我仍然得到一条未覆盖的线。所有的中间件等使得实际测试该功能不值得付出努力。

jestjs code-coverage esbuild
1个回答
1
投票

添加解析提示评论忽略函数的正确方法,但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

 的解析提示  来达到 100% 的覆盖率,这样它仍然会出现在 esbuild 的 output 中:

// 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.
© www.soinside.com 2019 - 2024. All rights reserved.