test-beside-subject
Where a unit test file sits, and what it is named.
Deterministic checkAdvises by default
The rule
Agents read this text as written when they write and review code.
Put a unit test beside the one source file it tests, and name it after that file: src/auth/AuthService.ts is tested by src/auth/AuthService.unit.test.ts. The part of the test's name before the first dot names a real file in the same folder.
- Never put a unit test in a
__tests__/,tests/ortest/folder undersrc/. - A test that covers several source files is split into one test file per source file.
- When one subject genuinely needs more than one test file, such as a pipeline with distinct scenarios, add a camelCase scenario to the name:
<File>.<scenario>.unit.test.ts, such asrunImplementPipeline.monorepo.unit.test.ts.
Then a file's test is found beside it, under its name.
Examples
The check flags the incorrect code and passes the correct code. Each example is a small repo, because this rule looks across files. It opens on the file that matters; the other files are the repo around it.
Incorrect
srcfeaturetests
import { expect, describe, test } from '@jest/globals';import { getLabel } from '../getLabel';// Incorrect: this test is filed into a separate folder, away from the file it tests.describe('getLabel', () => {test('trims the name it is given', () => {const label = getLabel({ name: ' Ada ' });expect(label).toBe('Ada');});});
Correct
srcfeature
import { expect, describe, test } from '@jest/globals';import { getLabel } from './getLabel';// Correct: the test sits beside `getLabel.ts` and is named after it.describe('getLabel', () => {test('trims the name it is given', () => {const label = getLabel({ name: ' Ada ' });expect(label).toBe('Ada');});});
Configure
- Block
"blocking"Stops a run when a file the run changed breaks the rule. - AdviseDefault
"advisory"Reports it and hands it to the refactor agent. Never stops a run. - Off
"off"Not checked. Use it when your own linter already enforces the rule.
Add this to your lightsout.config.json, then change the value.
{"standards-rule-settings": {"test-beside-subject": "advisory"}}