Course contentsShow
← JavaScript
Lesson 2658 of 2,72354. Testing in JavaScriptFree lesson

Test Discovery and Patterns

How test runners find test files using glob patterns, naming conventions, and configuration options.

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 .js file 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.js file in any directory
  • src/**/*.spec.js — Any .spec.js file under src/
  • test/**/* — Everything inside the test/ 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.