[英]Ignore certain files when running `npm test`
Currently npm test
is running all files that has .test.js
extension.目前npm test
正在运行所有具有.test.js
扩展名的文件。 I would like some files to be ignored.我想忽略一些文件。 Where do I configure that option?我在哪里配置该选项? I tried我试过
"jest": {
"collectCoverageFrom": [
"src/App.test.js"
]
},
in package.json.在 package.json 中。 I don't see any difference.我看不出有什么区别。
package.json
only allows you to override the following Jest configuration with create-react-app
package.json
仅允许您使用create-react-app
覆盖以下 Jest 配置
"jest": {
"collectCoverageFrom": [],
"coverageThreshold": {},
"coverageReporters": [],
"snapshotSerializers": []
}
Eject Create React App and configure by using testPathIgnorePatterns
.弹出 Create React App 并使用testPathIgnorePatterns
配置。
You can still override Jest's configuration by passing the --testPathIgnorePatterns
option to react-scripts test
.您仍然可以通过将--testPathIgnorePatterns
选项传递给react-scripts test
来覆盖 Jest 的配置。
For example:例如:
"test": "react-scripts test --testPathIgnorePatterns=src/ignoredDirectory --env=jsdom"
You might be tempted to use testPathIgnorePatterns
as a cli argument, but note this causes unexpected side effects, in that it breaks the ability to run single tests with npm test TestName
or npm test -- -t TestName
.您可能想使用testPathIgnorePatterns
作为 cli 参数,但请注意,这会导致意想不到的副作用,因为它会破坏使用npm test TestName
或npm test -- -t TestName
运行单个测试的能力。
This is because, as far as i can tell, testPathIgnorePatterns
is incredibly greedy, in that any argument after the testPathIgnorePatterns
argument will be used as a value for testPathIgnorePatterns
.这是因为,据我所知, testPathIgnorePatterns
非常贪婪,因为testPathIgnorePatterns
参数之后的任何参数都将用作testPathIgnorePatterns
的值。
For example:例如:
If I have the following set in my package.json
:如果我的package.json
中有以下设置:
"test": "react-scripts test --testPathIgnorePatterns=e2e",
And then run: npm test MyTest
然后运行: npm test MyTest
Then the resulting command is: react-scripts test --testPathIgnorePatterns=e2e "App"
然后生成的命令是: react-scripts test --testPathIgnorePatterns=e2e "App"
And jest will be ignore both e2e
and App
!而 jest 将忽略e2e
和App
!
A workaround I have found is to not use the testPathIgnorePatterns
cli arg, and instead use the testMatch
jest config.我发现的解决方法是不使用testPathIgnorePatterns
cli arg,而是使用testMatch
jest 配置。
Let's say I want to ignore all files in an e2e
directory, then I could use the following regex:假设我想忽略e2e
目录中的所有文件,那么我可以使用以下正则表达式:
"testMatch": [
"<rootDir>/src/(?!e2e)*/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/(?!e2e)*/**/*.{spec,test}.{js,jsx,ts,tsx}"
]
Or if i wanted to only include tests in a certain directory, I can use:或者如果我只想在某个目录中包含测试,我可以使用:
"testMatch": [
"<rootDir>/src/modules/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/modules/**/*.{spec,test}.{js,jsx,ts,tsx}"
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.