Parameterised Test With an Empty Dataset
ID |
javascript.parameterized_test_empty_dataset |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
JavaScript |
Tags |
reliability, testing |
Description
Reports a parameterised declaration whose dataset is written empty: it.each([])(…), describe.each([])(…), or an each template table with a header row and no data rows.
Only a dataset written empty at the call is reported. A dataset built elsewhere — a name, a spread, a call — may well be empty at run time, but the source does not say so, and a rule that guessed would report every table-driven suite that keeps its cases in a fixture.
Rationale
// Bad
it.each([])('rejects %s', (input) => {
expect(() => parse(input)).toThrow();
});
test.each`
input | expected
`('parses $input', ({ input, expected }) => {
expect(parse(input)).toBe(expected);
});
-
The runner has nothing to run the body against, so it registers no test.
-
It reports no skip either, because from the runner’s point of view nothing was ever declared — the file simply contributes fewer tests than it appears to.
-
The body reads as covered: the assertions are written, reviewed and maintained, and never execute.
-
It is the usual result of emptying a table to reproduce one case, or of a table that was going to be filled in later.
Remediation
Write the cases, or remove the declaration until there are some.
// Good
it.each([['', 'empty input'], ['x1', 'unexpected character']])(
'rejects %s',
(input, reason) => {
expect(() => parse(input)).toThrow(reason);
}
);
// Good — a template table with its rows
test.each`
input | expected
${'1'} | ${1}
${'2'} | ${2}
`('parses $input', ({ input, expected }) => {
expect(parse(input)).toBe(expected);
});