Test File Without a Test Case
ID |
javascript.test_file_without_test |
Severity |
critical |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
JavaScript |
Tags |
coverage, reliability, testing |
Description
Reports a file that a test runner collects as a spec — its name carries a .test. or .spec. infix, it sits in a tests directory, or it imports a test framework — yet declares no test case.
A file holding only a describe(…) is reported too: a suite with no it in it runs no test, and is the commonest way this happens, when every test of a group is commented out or moved away and the empty group is left behind.
What counts as a declaration is the call, not the file’s shape: an it(…) nested in a suite, in a loop, or in a helper called from the file all count, because all three register a test with the runner.
Rationale
// Bad — collected as a spec, runs nothing
import { describe, beforeEach } from 'vitest';
import { loadCart } from '../cart';
describe('cart', () => {
beforeEach(() => {
loadCart();
});
});
-
The runner reports the file as passing, because a file with no test has no failures.
-
Coverage of everything the file was written to cover silently drops to nothing, and no gate fires on it.
-
A suite whose tests were all commented out during a refactor reads, in review and in the run’s output, exactly like one that still covers its subject.
-
The file’s own setup — imports, fixtures, hooks — keeps being maintained by everyone who touches the module, for no return.
Remediation
Declare the tests, or delete the file. If the group is a work in progress, mark it explicitly (describe.skip, it.todo) so the run says so.
// Good
import { describe, it, expect, beforeEach } from 'vitest';
import { loadCart, total } from '../cart';
describe('cart', () => {
let cart;
beforeEach(() => {
cart = loadCart();
});
it('starts empty', () => {
expect(total(cart)).toBe(0);
});
});
A spec whose whole content is a call to a suite factory imported from another file (runSharedSuite(config)) does register tests, but from code this rule cannot see, and is reported. Either declare one test in the file or move the factory call into a describe the reader can recognise.