javascript 指定 jest 测试文件目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52637116/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:59:49  来源:igfitidea点击:

Specify jest test files directory

javascriptjestjs

提问by Geoff

Am new to unit testing and i wanted only to test files located in a specific directory only

我是单元测试的新手,我只想测试位于特定目录中的文件

How do i specify that i want tests to run to files only on a specific directory and ignore others

我如何指定我希望测试仅对特定目录上的文件运行而忽略其他文件

so i have installed jest via npm

所以我通过 npm 安装了 jest

  "jest": "^23.6.0",

And specified my test command in package.json via

并通过在 package.json 中指定我的测试命令

scripts:{
    "test": "jest --verbose"
 }

The above runs all the files but i want it to run for files in a specific directory eg ?laratest directory only

以上运行所有文件,但我希望它运行特定目录中的文件,例如仅 ?laratest 目录

How do i proceed

我该如何进行

回答by FuzzyTree

Add the directory name as an argument

添加目录名称作为参数

scripts:{
    "test": "jest --verbose ./my-directory"
}

回答by CodeDraken

Add configuration to your package.json.

将配置添加到您的 package.json。

"jest": {
  "testMatch": ["**/laratest/**/*.test.js"]
}

https://jestjs.io/docs/en/configuration.html#testmatch-arraystring

https://jestjs.io/docs/en/configuration.html#testmatch-arraystring

回答by Kimball Robinson

This article

本文

https://bambielli.com/til/2018-09-09-moving-jest-config-out-of-root/

https://bambielli.com/til/2018-09-09-moving-jest-config-out-of-root/

suggests doing something like this in your config.js file, and I quote the article:

建议在你的 config.js 文件中做这样的事情,我引用了这篇文章:

// jest.conf from inside `config/` directory
{
  rootDir: '../',
  globalSetup: {...},
}