> codebench_TOOL: HomeLOCAL-ONLY⌘K Switch

Glob Patterns: Why * Misses Nested Tests and ** Finds Them

Move a test one folder deeper and a passing build can stop running it. Compare three glob patterns on the same six paths and see exactly which tests they select.

·2 min readAbout the maintainer
A test file sits three levels down a directory tree, beyond the reach of the pattern tests/*.spec.ts.

Your build still passes after you move menu.spec.ts from tests/ into tests/ui/. Then you notice its name is gone from the test report. The file is fine. The runner was asked for tests/*.spec.ts, and the move introduced a slash where the * can only match part of a filename.

Try the move without touching a repository

Open the Glob Tester. Paste these six lines into File Paths, one path per line. Leave Match dotfiles, Case insensitive, and .gitignore mode off. The paths are relative to an imaginary project root; you don't need to create the files.

tests/cart.spec.ts
tests/ui/menu.spec.ts
tests/ui/mobile/nav.spec.ts
tests/.scratch/draft.spec.ts
tests/ui/menu.test.ts
src/cart.spec.ts

Enter the first pattern below in Glob Patterns and choose Test. For each subsequent run, replace the pattern rather than adding another line. You can keep the results open and edit the pattern on the left.

Pattern Files selected from the list
tests/*.spec.ts tests/cart.spec.ts
tests/*/*.spec.ts tests/ui/menu.spec.ts
tests/**/*.spec.ts tests/cart.spec.ts, tests/ui/menu.spec.ts, tests/ui/mobile/nav.spec.ts

The first two runs both say Matched: 1/6 paths. Look at which row has the check mark. Adding */ did not widen the first pattern. It traded “directly inside tests/” for “inside exactly one subdirectory of tests/.” A count alone would hide that mistake.

Read the slashes before the stars

A single * matches zero or more characters within one path segment. In *.spec.ts it can cover cart, but not ui/menu. In the third pattern, ** occupies a whole segment of its own. Here **/ can stand for zero or more intervening directories. Zero is the important case: the recursive pattern keeps the original cart.spec.ts as well as picking up the nested tests.

In tests/**/*.spec.ts the middle **/ matches nothing for tests/cart.spec.ts and matches ui/mobile/ for tests/ui/mobile/nav.spec.ts.
Read from both ends: the root is still tests/ and the filename must still end in .spec.ts. Only the middle can vary in depth.

Decide what should stay out

Keep tests/**/*.spec.ts in the editor, enable Match dotfiles, and choose Test again. The count rises from 3/6 to 4/6. The new row is tests/.scratch/draft.spec.ts: the leading dot on the directory kept the earlier wildcard from matching it. The dots inside menu.spec.ts do not make that file hidden.

Two paths still stay out, for different reasons. tests/ui/menu.test.ts has the wrong suffix; src/cart.spec.ts starts at the wrong root. Keep examples like these alongside the files you expect to include. They tell you if a “fix” such as **/*.ts would also pull unrelated source files into the test run.

Check who interprets the pattern

The browser tool uses picomatch to match the strings you paste; it doesn't walk a filesystem. Your runner may start from a package directory, apply its own exclusions, or use a different glob dialect. In a POSIX-style shell, quote a pattern meant for the runner, for example 'tests/**/*.spec.ts', so the shell doesn't expand it first. In the tester, enter the pattern without those quotes.

Before committing the change, use your runner's test-listing mode, if it has one. Look for both the moved menu.spec.ts and the original cart.spec.ts, and check that the scratch file is absent if that's your policy. Seeing the right filenames is stronger evidence than another green build.

#glob#testing#build-tools
> codebench $Ctrl+Enter Run