忽略 "ignore" 提示

Ignoring "ignore" hints

我正在尝试向 Jest/Istanbul 提供忽略提示,但它们被完全忽略了 - 覆盖范围没有改变。例如:

/* istanbul ignore if */
if (process.env.NODE_ENV === 'development') {
    const fromStateStr = JSON.stringify(fromState);
    const toStateStr = JSON.stringify(toState);
    console.log(
        `RouterStore.transition(${fromStateStr}, ${toStateStr})`
    );
}

我也试过 /* istanbul ignore next */ 个别线路,但没有任何变化。我错过了一些设置吗?这是我的 Jest 配置:

"jest": {
  "transform": {
    ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
  },
  "testRegex": "(/__tests__/.*|\.(test|spec))\.(ts|tsx|js)$",
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js"
  ],
  "setupFiles": [
    "./test/test-shim.ts",
    "./test/test-setup.ts"
  ],
  "coveragePathIgnorePatterns": [
    "/node_modules/",
    "/test/"
  ],
  "coverageThreshold": {
    "global": {
      "branches": 90,
      "functions": 95,
      "lines": 95,
      "statements": 95
    }
  },
  "collectCoverage": true,
  "mapCoverage": true
}

这是因为 TypeScript 编译器删除了所有注释,包括 Istanbul 提示。因此测试忽略了所有提示。必须将 "removeComments": false 添加到 tsconfig.json 才能完成这项工作。

P.S。感谢 Lucian Lature 指出这一点。