noctcore-architecture/colocated-test-required
noctcore-architecture/colocated-test-required
- Package
- architecture v1.1.0
- Preset
- none opt-in
- Severity
- off
- Autofix
- No
- Suggestions
- No
- Options
- Type info
- Not needed
A source file matching an include glob must have a colocated *.test.* / *.spec.* sibling on disk.
Some files are risky enough that shipping them untested should be a lint error, not a code-review afterthought — hooks, services, reducers, money math. This rule lets you name those globs and then requires each matching file to carry its test right next to it, so the test travels with the code and is obvious when missing.
In recommended
Section titled “In recommended”Left out of recommended. There is no universal “everything needs a colocated test” default, so the
recommended preset cannot safely enable it — turn it on with your own include globs.
What it flags
Section titled “What it flags”For a file matching include, the rule reads the file’s directory and looks for a sibling whose name
is <stem>.test.<ext> or <stem>.spec.<ext> (any extension). If none exists, it reports.
src/hooks/useCart.ts ← include: ['**/use*.ts']src/hooks/useCart.test.ts ✓ colocated test presentsrc/hooks/useWishlist.ts ✗ no useWishlist.test.* / .spec.* siblingDirectory listings are cached for the lint run, so a folder full of gated files is read once.
What it does not flag
Section titled “What it does not flag”- Anything, until
includeis configured: with no globs the rule never fires. - Files that do not match
include, and files matched byallowIn. - A test file that itself matches
include: it is never asked to test itself. - A gated file with either a
.test.*or a.spec.*sibling.
Edits to other files
Section titled “Edits to other files”This rule also reads the source file’s directory listing, so its result can change when that file
does. eslint --cache does not notice and keeps the old result until the linted file itself
changes. A long-running editor server keeps the first read until it restarts, so restart it after
changing that file. See Rules that read other
files.
Options
Section titled “Options”| Option | Type | Default | Meaning |
|---|---|---|---|
include |
string[] |
[] |
Globs of source files that must have a colocated test. Empty = the rule is off. |
allowIn |
string[] |
[] |
Globs of files to exempt even when they match include. |
ignore |
string[] |
(none) | Deprecated alias of allowIn, kept through 1.x. When both are set, their globs are combined. |
'noctcore-architecture/colocated-test-required': ['error', { include: ['**/use*.ts', '**/*.service.ts', '**/*.reducer.ts'], allowIn: ['**/*.d.ts'],}]When not to use it
Section titled “When not to use it”If your tests live in a separate __tests__ tree or a top-level test/ directory rather than beside
the source, this colocation check does not model your layout. Leave it off.