指定jest test files目录

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)

  • 这就是我需要的,要添加到 `jest.config.js` 的配置片段如果您想将其发布为专门针对 `jest.config.js` 配置的问题的答案,我将发布一个问题并发送给您。 (2认同)

Fuz*_*ree 19

添加目录名作为参数

scripts:{
    "test": "jest --verbose ./my-directory"
}
Run Code Online (Sandbox Code Playgroud)

  • 只是为了帮助像我这样的其他玩笑初级者,所有测试文件都必须以 int '.test.js' 结尾,否则玩笑将找不到它们。 (3认同)
  • 这还将包括名为“my-directory”的任何子目录。 (3认同)

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


Eri*_*son 8

[编辑:如其他答案中所述,根测试目录可以是一个参数(例如jest ... laratest),截至原始写作,这不起作用]

--rootdir选项添加到您的命令中:

jest --verbose --rootDir=laratest

或者在脚本中:

scripts:{
    "test": "jest --verbose --rootDir=laratest"
 }
Run Code Online (Sandbox Code Playgroud)

正如@Dimitri 所指出的,这会将 laratest 视为配置位置,而不仅仅是测试位置。对于这种情况,只需添加参数即可。


Kim*_*son 6

本文

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)