Jest finds tests by scanning your project for files that match a specific naming pattern. This lesson explains those conventions, how to configure them, and practical ways to organize test files as a project grows.
How Jest Recognizes a Test File
By default, Jest treats any file matching **/__tests__/**/*.[jt]s?(x) or **/?(*.)+(spec|test).[jt]s?(x) as a test file. In plain terms: anything inside a __tests__ folder, or any file ending in .test.js, .spec.js, .test.ts, or .spec.ts (and their .tsx/.jsx variants).
You rarely need to memorize the exact glob pattern — following the *.test.js convention next to your source files, or grouping tests in __tests__ folders, covers almost every real project.
testMatch overrides which file patterns Jest treats as tests.
testPathIgnorePatterns excludes folders like node_modules or build output.
roots limits which top-level directories Jest scans at all.
Most projects never need to touch these — the defaults are usually correct.
Test File Naming Cheat Sheet
Common ways to name and place Jest test files.
Pattern
Example
Colocated test
formatDate.js + formatDate.test.js
Spec suffix
user.spec.js
Dedicated folder
__tests__/formatDate.js
TypeScript test
formatDate.test.ts
React component test
Button.test.jsx
Ignore a folder
testPathIgnorePatterns: ['/dist/']
Colocated Files vs. a Dedicated __tests__ Folder
Colocating a test file right next to the source file it tests (Button.jsx and Button.test.jsx in the same folder) makes it obvious which code is tested and keeps related files together during refactors and deletions.
A dedicated __tests__ folder groups all tests separately, which some teams prefer for a cleaner source tree, especially in backend projects with many small utility files.
Colocated: easiest to find, moves/deletes naturally with the source file.
Dedicated folder: keeps src/ free of test files, useful for large codebases.
Pick one convention per project and apply it consistently.
Running a Subset of Test Files
As a project grows, running the entire suite for every change becomes slow. Jest lets you target specific files or folders directly from the CLI.
npx jest src/utils # run every test file under src/utils
npx jest formatDate # run test files whose path matches "formatDate"
npx jest --listTests # print every file Jest would run, without running it
Common Mistakes
Naming a file formatDate.tests.js (plural) instead of formatDate.test.js.
Placing test files inside dist/ or build/ and having Jest accidentally pick up compiled output.
Overriding testMatch without also keeping node_modules excluded, slowing down every run.
Mixing colocated and __tests__-folder conventions inconsistently across the same codebase.
Key Takeaways
Jest automatically finds .test.js/.spec.js files and anything in __tests__ folders.
testMatch and testPathIgnorePatterns let you customize discovery when needed.
Colocated test files and dedicated __tests__ folders are both valid; consistency matters most.
You can run a subset of tests by passing a file or folder path to the jest CLI.
Pro Tip
Run npx jest --listTests in an unfamiliar codebase before writing new tests. It shows you exactly which files Jest considers tests, which quickly reveals the project's naming convention.
You now understand Jest's test file conventions. Next, learn the difference between describe(), it(), and test() and how to organize tests with them.