Test Discovery and Patterns
You'll learn: how test runners automatically locate and execute your test files using naming conventions, glob patterns, and configuration settings.
What Is Test Discovery?
Test discovery is the process by which a test runner scans your project to find which files contain tests. Instead of manually listing every test file, runners use patterns — like looking for files ending in .test.js or .spec.js — to automatically detect what should run.
Think of it like a library's cataloging system: books aren't randomly shelved, they follow patterns (genre, author name) that make them discoverable. Similarly, test files follow conventions that runners recognize.
Common Naming Conventions
Most test runners look for files matching patterns like:
*.test.js— Files ending in.test.js*.spec.js— Files ending in.spec.js__tests__/**/*.js— Any.jsfile inside a__tests__folder
For example, if you have math.js, you might name its tests math.test.js or place them in __tests__/math.js.
Glob Patterns
Test runners use glob patterns to match file paths. Common patterns include:
**/*.test.js— Any.test.jsfile in any directorysrc/**/*.spec.js— Any.spec.jsfile undersrc/test/**/*— Everything inside thetest/folder
The ** wildcard matches any number of nested directories.
Configuration Options
You can customize discovery in your test runner's config file. For instance, Jest might use testMatch to specify patterns, Vitest uses include, and Node's test runner accepts patterns via CLI flags.
This flexibility lets you adapt to your project's structure without renaming files.
Key Takeaway: Test discovery uses naming conventions and glob patterns to automatically find test files, reducing manual configuration and ensuring consistency across your test suite.