简体   繁体   English

开玩笑 没有找到测试

[英]Jest No Tests found

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 --watchAll I get the below output node_modules/.bin/jest --watchAll我得到以下 output

No tests found没有找到测试
In /usr/src/app在 /usr/src/app
5 files checked.检查了 5 个文件。
testMatch: /__tests__/ /*.js?(x),**/?(*.)(spec|test).js?(x) - 1 match testMatch: /__tests__/ /*.js?(x),**/?(*.)(spec|test).js?(x) - 1 场比赛
testPathIgnorePatterns: /node_modules/,/src, src - 0 matches testPathIgnorePatterns: /node_modules/,/src, src - 0 个匹配项
Pattern: "" - 0 matches模式:“” - 0 场比赛

I've re-installed the package numbers times but to no avail.我已经重新安装了 package 号,但无济于事。

Your output says that testMatch had 1 match , which may be your __tests__/index.test.js file.您的输出表明testMatch1 match ,这可能是您的__tests__/index.test.js文件。 It seems that your testPathIgnorePatterns is causing that test suite to be ignored.您的testPathIgnorePatterns似乎导致该测试套件被忽略。 No tests found In /usr/src/app says that Jest is looking for tests in /usr/src/app , and testPathIgnorePatterns: /node_modules/,/src,src says that Jest is ignoring files in /src directories. No tests found In /usr/src/app说,玩笑正在寻找在测试中/usr/src/app ,并testPathIgnorePatterns: /node_modules/,/src,src说,玩笑被忽略的文件/src目录。

Either point Jest to look at the location of your __tests__/index.test.js file if it is outside the /src directory, or stop testPathIgnorePatterns from ignoring the /src directory.要么指向 Jest 查看__tests__/index.test.js文件的位置(如果它在 /src 目录之外),要么阻止testPathIgnorePatterns忽略 /src 目录。

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.js the following pattern for testMatch property:那么你需要在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

如果你想运行测试文件夹中的所有测试,你可以简单地执行以下jest __tests__ --watch

You can try running jest without any parameters.您可以尝试在没有任何参数的情况下运行jest A key thing to note is the test file names have to follow the convention *.test.js or *.spec.js .需要注意的关键是测试文件名必须遵循约定*.test.js*.spec.js

In package.json there is a pattern that is used to find test file.在 package.json 中有一个用于查找测试文件的模式。 In this pattern, you can change it according to your file location or make it the global pattern.在此模式中,您可以根据文件位置更改它或使其成为全局模式。

I wrote a test, not in the specific folder and follow a naming convention *.test.js and change testMatch我写了一个测试,不在特定文件夹中并遵循命名约定*.test.js并更改testMatch

"testMatch": [
      "<rootDir>/src/**/*.(test).{js,jsx,ts,tsx}",
      "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}"
    ],

Moving the __tests__ filter into src fixed the issue for me.__tests__过滤器移动到src为我解决了这个问题。 Now its able to run the tests.现在它能够运行测试。

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.通过在与主项目不同的文件夹树中单独测试子模块来修复该问题。

If you're using Typescript, I started getting the same No tests found error after I updated Typescript to 3.8.3 from 3.3.如果您使用的是 Typescript,在我将 Typescript 从 3.3 更新到 3.8.3 后,我开始收到相同的No tests found错误。 Updating jest and ts-jest from version 23.* to 25.* fixed it for me.将 jest 和 ts-jest 从版本 23.* 更新到 25.* 为我修复了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM