How to Resolve the "Your test suite must contain at least one test" Error in Jest in JavaScript
The Jest error Your test suite must contain at least one test is a common issue that occurs when Jest runs a file that it believes is a test but finds no actual tests inside. This usually happens for one of three reasons: an empty or incomplete test file, a misnamed utility file that Jest mistakes for a test, or an incorrect import.
This guide will walk you through the common causes of this error and provide a clear, step-by-step process to identify and fix the problem.
The Core Problem: Jest Found a "Test File" With No Tests
By default, Jest uses a set of patterns to discover test files. It looks for:
- Files inside any directory named
__tests__. - Files with a
.test.js(or.spec.js) suffix (e.g.,App.test.js).
When Jest finds a file that matches one of these patterns, it expects that file to contain at least one test() or it() block. If it doesn't, it throws the "Your test suite must contain at least one test" error.
Cause 1 (Most Common): The Test File is Empty or Incomplete
This is the most frequent cause of the error. You may have created a new test file but haven't written any tests in it yet, or you may have a describe block without a test block inside it.
Example of code with
// App.test.js - This file will cause the error.
// This describe block contains an assertion, but it's not inside a test() or it() block.
describe('App component', () => {
expect(1 + 1).toBe(2); // ⛔️ An assertion outside a test block
});
Jest does not consider an assertion by itself to be a test.
Solution: ensure every test file contains at least one test() (or it()) block, and that your assertions are inside it.
// App.test.js - ✅ This is correct.
describe('App component', () => {
// The test() block is the actual test unit.
test('should correctly perform addition', () => {
expect(1 + 1).toBe(2);
});
});
Cause 2: A Non-Test File is Mistaken for a Test
This often happens when you create a helper or utility file and give it a name that Jest's pattern matching accidentally picks up.
For example,Imagine you create a utility file for your tests and place it inside your __tests__ directory.
__tests__/
├── App.test.js
└── test-helpers.js // ⛔️ Jest thinks this is a test file!
Since test-helpers.js is inside __tests__ but contains no test() blocks, Jest will throw the error for this file.
Solution: there are two ways to fix this:
- Rename the File (Recommended): Rename your helper files so they don't match Jest's patterns. For example, move it to a
__mocks__orutilsdirectory outside the__tests__folder. - Configure Jest to Ignore It: If you cannot rename it, you can explicitly tell Jest to ignore the file.
Cause 3: Incorrectly Importing Jest Globals
Jest automatically makes its global functions (describe, test, it, expect) available in all test files. A common mistake, especially with auto-importing IDEs, is to accidentally import these from another library, which overrides Jest's globals and breaks the test runner.
Example of code with errors:
// ⛔️ Your IDE might have added this line, which is incorrect.
import { describe, test, expect } from '@jest/globals';
// Or even worse, from a different library like 'vitest' or 'mocha'.
describe('My test suite', () => {
// ...
});
When these are imported, Jest no longer recognizes the file as containing valid tests, leading to the "at least one test" error.
Solution: Delete the incorrect import statements. The Jest globals should never be imported. If your editor is having trouble with type definitions, install the official Jest types:
npm install --save-dev @types/jest
Advanced Solution: Configuring Jest to Ignore Files
If you have files that must be located in a __tests__ directory but are not tests, you can use the testPathIgnorePatterns configuration in your jest.config.js file to exclude them.
Let's say you want Jest to ignore any file ending in -helper.js.
jest.config.js:
module.exports = {
// ... other config
testPathIgnorePatterns: [
'/node_modules/', // It's good practice to always ignore node_modules
'-helper\\.js$', // Ignore files ending in -helper.js
],
};
This tells Jest's test runner to skip any file whose path matches this regular expression.
Conclusion
The "Your test suite must contain at least one test" error is Jest's way of telling you it found a file it expected to be a test but wasn't.
To solve it, follow these steps:
- Check the file mentioned in the error message. Is it empty or missing a
test()orit()block? If so, add one. - If the file is not supposed to be a test, rename it or move it so it no longer matches Jest's test file patterns (
__tests__/or*.test.js). - Ensure you have not accidentally imported any of Jest's global functions (
describe,test,it,expect). - As a last resort, use
testPathIgnorePatternsin your Jest config to explicitly exclude files.