Jest finds no test files and exits with code 1
No tests found, exiting with code 1Short answer
Jest exits with code 1 when its file scan matches nothing. List what it would run with jest --listTests. If that is empty, the file names or folders do not match testMatch, roots or rootDir is wrong, or testPathIgnorePatterns hides them. In watch mode, Jest runs only changed files and needs a git or hg repository.
Fix it fast
1Find out which cause you have
Ask Jest which test files it sees, with the same options you normally pass.
npx jest --listTests2Match what you see, then jump to its fix
- 1
--listTests prints nothing though test files existFile names or folders do not match testMatchFix - 2
Tests live outside the folder Jest scansroots or rootDir points at the wrong folderFix - 3
Tests sit under a path matched by an ignore patterntestPathIgnorePatterns hides the filesFix - 4
npm test works with --watchAll but not with --watch, or no changed files existWatch mode in a folder that is not a git or hg repositoryFix - 5
A new package with no tests yet fails the CI stepExit code 1 is wanted behaviour for an empty projectFix
- 1
What does the error look like?
No tests found, exiting with code 1
Cause 1 of 5
File names or folders do not match testMatch
1Check
By default Jest runs files in __tests__ folders and files named *.test.* or *.spec.* with js, jsx, ts, tsx (and mjs, cjs, mts, cts) extensions.
npx jest --showConfig | grep -A4 testMatch2Fix
Rename the files to match, or set testMatch in the config.
// jest.config.js
module.exports = {
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
};3Verify
The file now appears.
npx jest --listTestsConfirmed by jestjs.io
Cause 2 of 5
roots or rootDir points at the wrong folder
1Check
Jest searches the directories in roots, and <rootDir> defaults to the config file's directory, else the package.json directory, else the current directory.
npx jest --showConfig | grep -E "rootDir|roots"2Fix
Run Jest from the project root, or set roots and rootDir to include the test folder.
module.exports = {
rootDir: '.',
roots: ['<rootDir>/src'],
};3Verify
List the tests again.
npx jest --listTestsConfirmed by jestjs.io
Cause 3 of 5
testPathIgnorePatterns hides the files
1Check
A match with testPathIgnorePatterns skips the file; the default is /node_modules/.
npx jest --showConfig | grep -A3 testPathIgnorePatterns2Fix
Remove or narrow the pattern that matches your test path.
module.exports = {
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/build/'],
};3Verify
List the tests again.
npx jest --listTestsConfirmed by jestjs.io
Cause 4 of 5
Watch mode in a folder that is not a git or hg repository
1Check
--watch reruns tests related to changed files. The Jest CLI docs say identifying changed files (--onlyChanged) works only in a git or hg repository.
git status2Fix
Use --watchAll to run all tests, or initialise a repository.
npx jest --watchAll3Verify
Jest now lists and runs the tests.
npx jest --watchAll=falseConfirmed by jestjs.io
Cause 5 of 5
Exit code 1 is wanted behaviour for an empty project
1Check
The CLI docs say without --passWithNoTests Jest errors when nothing matches.
npx jest --listTests2Fix
Add the flag for packages that may have no tests yet.
npx jest --passWithNoTests3Verify
The command exits with code 0.
npx jest --passWithNoTests; echo $?Confirmed by jestjs.io
How do I stop it coming back?
- Run jest --listTests after changing config or moving folders.
- Name tests *.test.* or put them in __tests__.
- Run Jest from the folder that holds package.json.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix