目前npm test
正在运行所有扩展名为.test.js
的文件。我希望忽略一些文件。我在哪里配置这个选项?我试过了。
"jest": {
"collectCoverageFrom": [
"src/App.test.js"
]
},
在 package.json 文件中, 我没有看到任何区别。
目前npm test
正在运行所有扩展名为.test.js
的文件。我希望忽略一些文件。我在哪里配置这个选项?我试过了。
"jest": {
"collectCoverageFrom": [
"src/App.test.js"
]
},
在 package.json 文件中, 我没有看到任何区别。
package.json
只允许您使用 create-react-app
覆盖以下 Jest 配置。
"jest": {
"collectCoverageFrom": [],
"coverageThreshold": {},
"coverageReporters": [],
"snapshotSerializers": []
}
通过使用testPathIgnorePatterns
拆卸 Create React App 并进行配置。
您仍然可以通过将--testPathIgnorePatterns
选项传递给react-scripts test
来覆盖Jest的配置。
例如:
"test": "react-scripts test --testPathIgnorePatterns=src/ignoredDirectory --env=jsdom"
testPathIgnorePatterns
作为 cli 参数会破坏使用 npm test TestName
或 npm test -- -t TestName
运行一个测试的能力。 - badsyntaxtestPathIgnorePatterns
作为cli参数,但请注意,这会导致意外的副作用,因为它会破坏使用npm test TestName
或npm test -- -t TestName
运行单个测试的能力。
因为就我所知,testPathIgnorePatterns
非常贪婪,即在testPathIgnorePatterns
参数之后的任何参数都将用作testPathIgnorePatterns
的值。
例如:
如果我在我的package.json
中设置了以下内容:
"test": "react-scripts test --testPathIgnorePatterns=e2e",
然后运行:npm test MyTest
那么得到的命令是:react-scripts test --testPathIgnorePatterns=e2e "App"
jset将会忽略e2e
和App
!
我发现的一个解决方法是不使用testPathIgnorePatterns
的cli参数,而是使用testMatch
jest配置。
假设我想忽略e2e
目录中的所有文件,则可以使用以下正则表达式:
"testMatch": [
"<rootDir>/src/(?!e2e)*/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/(?!e2e)*/**/*.{spec,test}.{js,jsx,ts,tsx}"
]
如果我只想包含特定目录中的测试,可以使用:
"testMatch": [
"<rootDir>/src/modules/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/modules/**/*.{spec,test}.{js,jsx,ts,tsx}"
]
testMatch
的文档,供任何感兴趣的人查阅。链接:https://jestjs.io/docs/configuration#testmatch-arraystring - Joe SadoskitestPathIgnorePatterns
可以在 package.json
中进行配置,而无需更改 npm test
命令。 - Joe Sadoskipackage.json
中进行配置。 - BobtheMagicMoose
testPathIgnorePatterns
。 - Sahil Raj Thapa