繁体   English   中英

无法使用 jest.config 忽略特定文件

[英]Unable to ignore specific files with jest.config

试图忽略所有索引文件,特别是src/index.jsxsrc/reportWebVitals.js文件,但是我的覆盖命令仍然显示被覆盖的行。

我在正确的dev分支上的Github 存储库,这是一个问题。

在此处输入图像描述

根据文档,它应该像将文件添加到coveragePathIgnorePatternstestPathIgnorePatterns一样简单。

jest.config

module.exports = {
  testEnvironment: 'node',
  testEnvironmentOptions: {
    NODE_ENV: 'test',
  },
  restoreMocks: true,
  coveragePathIgnorePatterns: [
    'node_modules',
    'index.js',
    'index.jsx',
    'server/src/config',
    'server/src/app.js',
    'src/index.jsx',
    'src/reportWebVitals.js',
    'tests',
  ],
  coverageReporters: ['text', 'lcov', 'clover', 'html'],
  testPathIgnorePatterns: ['index.js', 'index.jsx', 'src/index.jsx', 'src/reportWebVitals.js'],
  roots: ['<rootDir>/server/tests'],
};

还在这里尝试了更长的版本:

module.exports = {
  testEnvironment: 'node',
  testEnvironmentOptions: {
    NODE_ENV: 'test',
  },
  restoreMocks: true,
  collectCoverageFrom: [
    'src/{!(index),}.jsx',
    'src/{!(reportWebVitals),}.js',
    'src/{!(store),}.js'
  ],
  coveragePathIgnorePatterns: [
    'node_modules',
    'index.js',
    'index.jsx',
    'server/src/config',
    'server/src/app.js',
    'index.jsx',
    'reportWebVitals.js',
    'store.js',
    'tests',
  ],
  coverageReporters: ['text', 'lcov', 'clover', 'html'],
  modulePathIgnorePatterns: [
    'node_modules',
    'index.js',
    'index.jsx',
    'server/src/config',
    'server/src/app.js',
    'index.jsx',
    'reportWebVitals.js',
    'store.js',
    'tests',
  ],
  watchPathIgnorePatterns: [
    'node_modules',
    'index.js',
    'index.jsx',
    'server/src/config',
    'server/src/app.js',
    'index.jsx',
    'reportWebVitals.js',
    'store.js',
    'tests',
  ],
  testPathIgnorePatterns: [
    'node_modules',
    'index.js',
    'index.jsx',
    'server/src/config',
    'server/src/app.js',
    'index.jsx',
    'reportWebVitals.js',
    'store.js',
    'tests',
  ],
  roots: ['<rootDir>/server/tests'],
};

我的 package.json 脚本

"client-dev": "react-scripts start",
"client-build": "react-scripts build",
"client-test": "react-scripts test ./src",
"client-coverage": "react-scripts test ./src --coverage",

更新:我注意到一件有趣的事情,我从 jest.config.js 中删除了所有忽略规则,并且覆盖范围仍然相同,node_modules 在覆盖范围内不是问题......所以现在探索我的项目是否正在挑选上配置。

module.exports = {
  testEnvironment: 'node',
  testEnvironmentOptions: {
    NODE_ENV: 'test',
  },
  restoreMocks: true,
  coverageReporters: ['text', 'lcov', 'clover', 'html'],
};

您必须在测试脚本中明确提及您的配置文件路径。 如果这样做,将导致此处讨论的另一个问题: https ://stackoverflow.com/a/68912023/10055300。 最好在package.json本身中包含 jest 配置,而不是为其创建一个单独的文件。

答案是我的 jest.config.js 被项目忽略了。 最初,当我开始处理这个项目时,这个配置已经包含在内,这就是为什么我保留它而不是在我的 package.json 中使用 jest 配置。

在使用 package.json 中的笑话规则后,忽略现在 100% 工作。

我不确定为什么 jest.config.js 不起作用,如果有人回答我会选择他们的答案并奖励他们 250 赏金积分。

"jest": {
  "coveragePathIgnorePatterns": [
    "node_modules",
    "server/src/config",
    "store.js",
    "index.jsx",
    "index.js",
    "tests"
  ],
  "watchPathIgnorePatterns": [
    "node_modules",
    "server/src/config",
    "store.js",
    "index.jsx",
    "index.js",
    "tests"
  ]
},

在此处输入图像描述

由于您使用的是未弹出的 create-react-app,因此覆盖 jest 配置(不弹出)的唯一方法是在父键jest下的package.json中添加允许的配置键。

像这样的东西:

{
  "jest": {
    "coveragePathIgnorePatterns": [
      "index.jsx"
    ],
  }
}

如果您浏览facebook/create-react-app/.../react-scripts/.../createJestConfig.js的源代码,您会看到 react-scripts 只允许使用一组固定的 jest 配置键被覆盖,并且仅使用 package.json 方法。 它不支持添加您自己的 jest.config.js 文件。 CRA(react-scripts) 创建自己的 jest 配置并使用它来运行您的测试。

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM