Geo*_*off 13 javascript jestjs
我是单元测试的新手,我只想测试位于特定目录中的文件
我如何指定我希望测试仅在特定目录上运行到文件而忽略其他目录
所以我通过npm安装了jest
"jest": "^23.6.0",
Run Code Online (Sandbox Code Playgroud)
并在package.json中指定了我的测试命令
scripts:{
"test": "jest --verbose"
}
Run Code Online (Sandbox Code Playgroud)
上面运行所有文件,但我希望它运行在特定目录中的文件,例如最新目录
我该怎么办?
ow3*_*w3n 23
在里面./jest.config.js
你可以添加一个目录数组来搜索
// A list of paths to directories that Jest should use to search for files in
roots: [
"./",
"../more-files/"
],
Run Code Online (Sandbox Code Playgroud)
Fuz*_*ree 19
添加目录名作为参数
scripts:{
"test": "jest --verbose ./my-directory"
}
Run Code Online (Sandbox Code Playgroud)
Cod*_*ken 13
将配置添加到package.json。
"jest": {
"testMatch": ["**/laratest/**/*.test.js"]
}
Run Code Online (Sandbox Code Playgroud)
https://jestjs.io/docs/zh-CN/configuration.html#testmatch-array-string
[编辑:如其他答案中所述,根测试目录可以是一个参数(例如jest ... laratest
),截至原始写作,这不起作用]
将--rootdir
选项添加到您的命令中:
jest --verbose --rootDir=laratest
或者在脚本中:
scripts:{
"test": "jest --verbose --rootDir=laratest"
}
Run Code Online (Sandbox Code Playgroud)
正如@Dimitri 所指出的,这会将 laratest 视为配置位置,而不仅仅是测试位置。对于这种情况,只需添加参数即可。
本文
https://bambielli.com/til/2018-09-09-moving-jest-config-out-of-root/
建议在你的 config.js 文件中做这样的事情,我引用了这篇文章:
// jest.conf from inside `config/` directory
{
rootDir: '../',
globalSetup: {...},
}
Run Code Online (Sandbox Code Playgroud)