Javascript 开玩笑 没有找到测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44741766/
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
Jest No Tests found
提问by Kendall ARNEAUD
running docker mhart/alpine-node:8 on macOS with
在 macOS 上运行 docker mhart/alpine-node:8
nodejs (6.10.3-r0) (18/18) yarn 0.24.6 jest 20.0.4
nodejs (6.10.3-r0) (18/18) 纱线 0.24.6 开玩笑 20.0.4
I have a __tests__/index.test.js file however, when running the code
但是,在运行代码时,我有一个 __tests__/index.test.js 文件
node_modules/.bin/jest --watchAllI get the below output
node_modules/.bin/jest --watchAll我得到以下输出
No tests found
In /usr/src/app
5 files checked.
testMatch: /__tests__//*.js?(x),**/?(*.)(spec|test).js?(x) - 1 match
testPathIgnorePatterns: /node_modules/,/src,src- 0 matches
Pattern: "" - 0 matches
在 /usr/src/app 中未找到测试
5 个文件已检查。
testMatch: /__tests__//*.js?(x),**/?(*.)(spec|test).js?(x) - 1 匹配
testPathIgnorePatterns: /node_modules/,/src, src- 0 匹配
模式: "" - 0 个匹配项
I've re-installed the package numbers times but to no avail.
我已经重新安装了软件包编号,但无济于事。
采纳答案by Zachary Ryan Smith
Your output says that testMatchhad 1 match, which may be your __tests__/index.test.jsfile. It seems that your testPathIgnorePatternsis causing that test suite to be ignored. No tests found In /usr/src/appsays that Jest is looking for tests in /usr/src/app, and testPathIgnorePatterns: /node_modules/,/src,srcsays that Jest is ignoring files in /srcdirectories.
您的输出显示testMatchhad 1 match,这可能是您的__tests__/index.test.js文件。似乎您testPathIgnorePatterns导致该测试套件被忽略。 No tests found In /usr/src/app说 Jest 正在寻找 中的测试/usr/src/app,并testPathIgnorePatterns: /node_modules/,/src,src说 Jest 正在忽略目录中的/src文件。
Either point Jest to look at the location of your __tests__/index.test.jsfile if it is outside the /src directory, or stop testPathIgnorePatternsfrom ignoring the /src directory.
__tests__/index.test.js如果文件位于 /src 目录之外,请指向 Jest 查看文件的位置,或者停止testPathIgnorePatterns忽略 /src 目录。
回答by Roman
If you have file structure such as the following
如果您有如下文件结构
myFolder
│ myFile1.js
│ myFile2.js
│ ...
│
└───__tests__
myFile1.spec.js
myFile2.spec.js
...
then you need to have in jest.config.jsthe following pattern for testMatchproperty:
那么您需要具有jest.config.js以下testMatch属性模式:
testMatch: ['**/__tests__/*.js?(x)'],
The simple example of jest.config.js:
简单的例子jest.config.js:
const jestConfig = {
verbose: true,
testURL: "http://localhost/",
'transform': {
'^.+\.jsx?$': 'babel-jest',
},
testMatch: ['**/__tests__/*.js?(x)'],
}
module.exports = jestConfig
回答by Abu Khadija
If you want to run all the tests inside the testsfolder you can simply do the following
jest __tests__ --watch
如果要运行测试文件夹中的所有测试,只需执行以下操作
jest __tests__ --watch
回答by mph
I had this error while attempting to run tests in a submodule of a project. Fixed the issue by testing the submodule in isolation, in a separate folder tree from the main project.
尝试在项目的子模块中运行测试时遇到此错误。通过在与主项目不同的文件夹树中单独测试子模块来修复该问题。
回答by Karuhanga
You can try running jestwithout any parameters. A key thing to note is the test file names have to follow the convention *.test.jsor *.spec.js.
您可以尝试jest不带任何参数运行。需要注意的关键是测试文件名必须遵循约定*.test.js或*.spec.js.
回答by Yawar Ali
In package.json there is a pattern that is used to find test file. In this pattern, you can change it according to your file location or make it the global pattern.
在 package.json 中有一个用于查找测试文件的模式。在此模式中,您可以根据文件位置更改它或使其成为全局模式。
I wrote a test, not in the specific folder and follow a naming convention *.test.jsand change testMatch
我写了一个测试,不在特定文件夹中,并遵循命名约定*.test.js并更改testMatch
"testMatch": [
"<rootDir>/src/**/*.(test).{js,jsx,ts,tsx}",
"<rootDir>/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}"
],
回答by What Would Be Cool
If you're using Typescript, I started getting the same No tests founderror after I updated Typescript to 3.8.3 from 3.3. Updating jest and ts-jest from version 23.* to 25.* fixed it for me.
如果您使用的是 Typescript,No tests found在将 Typescript 从 3.3 更新到 3.8.3 后,我开始收到相同的错误。将 jest 和 ts-jest 从版本 23.* 更新到 25.* 为我修复了它。

