javascript 仅对当前文件夹运行 Jest 测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49613127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run Jest tests only for current folder
提问by maverick
I have Jest installed on my machine and typing jestfrom terminal results in tests from parent folers also getting executed. I want to run tests only from the current folder.
我在我的机器上安装了 Jest 并jest从终端输入结果,来自父级的测试也得到执行。我只想从当前文件夹运行测试。
For e.g. if I go to c:/dev/appin terminal and type some-jest-command, it should only run files with .test.jspresent in the appfolder. Currently, running jestcommand from appfolder runs tests in parent folders too, which is not my desired behaviour.
例如,如果我进入c:/dev/app终端并输入some-jest-command,它应该只运行文件夹中.test.js存在的app文件。目前,jest从app文件夹运行命令也会在父文件夹中运行测试,这不是我想要的行为。
回答by Andrew Miroshnichenko
By default, Jest will try to recursively test everything from whatever folder package.jsonis located.
默认情况下,Jest 将尝试递归测试任何文件夹中的所有内容package.json。
Let's say you're in c:/dev/app, and your package.jsonis in c:. If your basic command to invoke Jest is npm test, then try with run npm test dev/app.
假设您在c:/dev/app,并且您package.json在c:。如果调用 Jest 的基本命令是npm test,请尝试使用 run npm test dev/app。
回答by Goran_Ilic_Ilke
To only run testing in a specific directory and to coerce Jest to read only certain type of files(my example: 'ExampleComponent.test.js' with new Jest version @24.9.0 you must write exact "testMatch" in jest.config.json || package.json in "jest" part next "testMatch": [
"<rootDir>/src/__tests__/**/*.test.js"
],
This testMatch in my case hits all files with the prefix .test.js in tests/subdirectories/ and skips all other files like 'setupTest.js' and other .js files in 'mocks' subdirectory which is placed inside of 'tests' directory,so,my 'jest.config.json' looks like this {
"setupFiles": [
"raf/polyfill",
"<rootDir>/setupTests.js"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"moduleNameMapper": {
"^.+\\.(css|less|scss|sass)$": "identity-obj-proxy"
},
"testMatch": [
"<rootDir>/src/__tests__/**/*.test.js"
]
}.
要仅在特定目录中运行测试并强制 Jest 仅读取某些类型的文件(我的示例:'ExampleComponent.test.js' 与新的 Jest 版本 @24.9.0,您必须在 jest.config 中写入精确的“testMatch”。 json || package.json in "jest" part next "testMatch": [
"<rootDir>/src/__tests__/**/*.test.js"
],在我的例子中,这个 testMatch 在测试/subdirectories/ 中命中所有带有前缀 .test.js 的文件,并跳过所有其他文件,如 'setupTest.js' 和其他 .js 文件' mocks' 子目录位于 ' tests' 目录中,所以,我的 'jest.config.json' 看起来像这样{
"setupFiles": [
"raf/polyfill",
"<rootDir>/setupTests.js"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"moduleNameMapper": {
"^.+\\.(css|less|scss|sass)$": "identity-obj-proxy"
},
"testMatch": [
"<rootDir>/src/__tests__/**/*.test.js"
]
}。
Just adapt to your needs 'testMatch' regex.
只需适应您的需求“testMatch”正则表达式。
A little note: This is for [email protected] && [email protected] if it matters to anyone.
一点注意:如果对任何人都很重要,这适用于 [email protected] &&酶@3.10.0。
I hope it will be useful to someone, cheers all.
我希望它对某人有用,大家欢呼。
回答by Sergiu Mare
If you want to run the tests from a specific folder user the --testPathPattern jest flag. When setting up the npm script add the path to the folder as well. In your package.json add the flag in you npm scripts. Check the bellow code for an example.
如果要从特定文件夹用户运行测试,请使用 --testPathPattern jest 标志。设置 npm 脚本时,还要添加文件夹的路径。在您的 package.json 中,在您的 npm 脚本中添加标志。检查波纹管代码以获取示例。
"scripts": {
....
"test:unit": "jest --watchAll --testPathPattern=src/js/tests/unit-tests",
"test:integration": "jest --watchAll --testPathPattern=src/js/tests/integration",
"test:helpers": "jest --watchAll jest --findRelatedTests src/js/tests/unit-tests/helpers/helpers.test.js"
....
},
After that open the command line, change your directory where your project is and run unit test.
之后打开命令行,更改项目所在的目录并运行单元测试。
npm run test:unit
or integration tests.
或集成测试。
npm run test:integration
or if you want a test only for one specific file run
或者如果您只想对一个特定文件运行进行测试
npm run test:helpers

