无法用jest.config忽略特定文件
试图忽略所有索引文件,但特别是src/index.jsx
和src/reportwebvitals.js
文件,但是我的coverage命令仍然显示出覆盖的行。
我的github repo DEV 分支这是一个问题。
根据文档,它应该像将文件添加到CoveragePathignorePatterns
和TESTPATHIGNOREPATTERNS
。
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'],
};
我的软件包。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'],
};
Trying to ignore all index files, but specifically the src/index.jsx
and src/reportWebVitals.js
files, however my coverage command still shows up covered lines.
My Github repo on the correct dev
branch where this is an issue.
According to the docs, it should be as simple as adding the file to coveragePathIgnorePatterns
and testPathIgnorePatterns
.
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'],
};
Also tried with a much longer version here:
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'],
};
My package.json scripts
"client-dev": "react-scripts start",
"client-build": "react-scripts build",
"client-test": "react-scripts test ./src",
"client-coverage": "react-scripts test ./src --coverage",
UPDATE: One interesting thing I noted, I removed all my ignore rules form the jest.config.js and the coverage is still the same, node_modules isn't a problem in the coverage... so now exploring if my project is even picking up the config.
module.exports = {
testEnvironment: 'node',
testEnvironmentOptions: {
NODE_ENV: 'test',
},
restoreMocks: true,
coverageReporters: ['text', 'lcov', 'clover', 'html'],
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须在测试脚本中明确提及您的配置文件路径。如果您这样做,它将导致另一个问题在这里讨论: https://stackoverflow.com/a/a/a/68912023/68912023/1005553300 。在
package.json
本身中拥有开玩笑的配置是理想的选择,而不是为其提供单独的文件。You will have to explicitly mention your config file path in your test script. If you do so, it will lead to another issue which has been discussed here: https://stackoverflow.com/a/68912023/10055300. It would be ideal to have jest configs in
package.json
itself rather than having a separate file for it.由于您使用的是未伸出的Create-React-app,因此覆盖开玩笑配置的唯一方法是在
package.json.json
下添加允许的配置键 。类似的内容:
如果您浏览 facebook/create-react-app /.../ react-scripts /.../ createJestConfig.js ,您会看到React-Script只允许一组固定的嘲笑配置键被覆盖,并且仅使用package.json方法。它不支持添加您自己的jest.config.js文件。 CRA(React-Scripts)创建了自己的嘲笑配置,并使用它来运行测试。
Since you are using an unejected create-react-app, the only way to override the jest configuration(without ejecting) is to add the allowed configuration keys in
package.json
under the parent keyjest
.Something like this:
If you go through the source code for facebook/create-react-app/.../react-scripts/.../createJestConfig.js, you will see that react-scripts allows only a fixed set of jest configuration keys to be overridden, and only using the package.json method. It does not support adding your own jest.config.js file. CRA(react-scripts) creates its own jest configuration and uses that to run your tests.
答案是该项目忽略了我的Jest.config.js。最初,当我开始从事此项目时,此配置已经包括在内,为什么我保留它,而不是在pockage.json的内部使用开玩笑的配置。
在包装中使用了开玩笑的规则后。JSON忽略现在正在100%工作。
我不确定为什么jest.config.js不起作用。
The answer was my jest.config.js is being ignored by the project. Initially when I started working on this project this config was already included which why I kept it rather than using jest config inside of my package.json.
After using the jest rules inside package.json the ignores are working 100% now.
I am unsure as to why the jest.config.js did not work.