Jest No Tests found
Asked Answered
U

10

61

running docker mhart/alpine-node:8 on macOS with

nodejs (6.10.3-r0) (18/18) yarn 0.24.6 jest 20.0.4

I have a __tests__/index.test.js file however, when running the code

node_modules/.bin/jest --watchAll I get the below output

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

I've re-installed the package numbers times but to no avail.

Unbelief answered 24/6, 2017 at 23:11 Comment(1)
The way I fixed this was by changing my docker-compose command slightly. I changed command: npm run cover && bash <(curl -s https://codecov.io/bash) to command: bash -c 'npm run cover && bash <(curl -s https://codecov.io/bash)'Carothers
B
24

Your output says that testMatch had 1 match, which may be your __tests__/index.test.js file. It seems that your testPathIgnorePatterns is causing that test suite to be ignored. 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.

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.

Bone answered 13/8, 2017 at 14:4 Comment(0)
S
23

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:

testMatch: ['**/__tests__/*.js?(x)'],

A simple example of jest.config.js:

const jestConfig = {
  verbose: true,
  testURL: "http://localhost/",
  'transform': {
    '^.+\\.jsx?$': 'babel-jest',
  },
  testMatch: ['**/__tests__/*.js?(x)'],
}

module.exports = jestConfig
Switchblade answered 19/1, 2019 at 4:36 Comment(0)
B
17

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.

I wrote a test, not in the specific folder and follow a naming convention *.test.js and change testMatch

"testMatch": [
      "<rootDir>/src/**/*.(test).{js,jsx,ts,tsx}",
      "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}"
    ],
Blockhouse answered 18/6, 2019 at 7:2 Comment(0)
O
8

You can try running jest without any parameters. A key thing to note is the test file names have to follow the convention *.test.js or *.spec.js.

Orthochromatic answered 25/11, 2018 at 0:38 Comment(1)
Is there a way to just use the .js extension instead of test.js or spec.js?Industrious
D
5

If you want to run all the tests inside the tests folder you can simply do the following jest __tests__ --watch

Dickerson answered 28/11, 2017 at 17:55 Comment(0)
E
4

Moving the __tests__ filter into src fixed the issue for me. Now its able to run the tests.

Ectogenous answered 23/2, 2022 at 3:4 Comment(0)
F
3

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.

Fabricant answered 14/8, 2018 at 10:2 Comment(2)
Could you elaborate on what this meant in practice?Bowfin
Shoot I don't remember now. Sounds like I either moved/copied the submodule folder out of the main project folder, or cloned it separately (if that's possible)Fabricant
C
1

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. Updating jest and ts-jest from version 23.* to 25.* fixed it for me.

Catchings answered 25/3, 2020 at 17:46 Comment(0)
I
1

For me i'm using the vscode plugin and the fix for me was in .vscode/settings.json add this setting right here

"jest.jestCommandLine": "node_modules\\.bin\\jest"
Instigation answered 21/8, 2023 at 5:21 Comment(0)
D
1

I've been wrestling with this annoying anonymous issue, but I find the tedious solution for it. if you type the test command like this npm test .\repeatString.spec.js or npm run test .\repeatString.spec.js you've done it right but it won't work.

I just removed .\ from the command and it worked perfectly try it without .\ before the file path

Dettmer answered 28/2 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.