Files
redefined-designs/frontend/tests/unit/findOrFail.test.ts
T
bermudalamb 64324609e1
Linting / lint (pull_request) Successful in 2m10s
SonarQube Analysis / sonarqube (pull_request) Successful in 19m45s
test(e2e): add a find-or-fail helper for collection lookups (#241)
Nine sites across five specs do `collection.find(...)` and dereference the result immediately. When the row is missing the test dies with "Cannot read properties of undefined" naming a line of test plumbing, which says nothing about what was expected — and that is exactly how favorites-filter:169 failed without producing a usable signal.

The message names what was wanted and how many rows were searched. That distinction carries real diagnostic weight: "0 rows" means the fixture never landed, "37 rows" means it landed and the predicate is wrong, and those are different bugs to chase.

It lives in its own module importing nothing, rather than in support/api.ts. That file imports @playwright/test, and vitest.config.ts runs tests/unit with environment: 'node' — putting six lines of pure logic there would drag a browser harness into the unit suite to test them. api.ts re-exports it so specs still reach it through fixtures.

Throws rather than returning null, because every caller wants the row: an error at the point of the miss beats a null threaded through three more lines before something unrelated fails.

Frontend: 30 unit tests pass, lint unchanged at 2 pre-existing warnings, build clean.

Ref #241
2026-08-30 17:02:07 -05:00

43 lines
1.2 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { findOrFail } from '../e2e/support/findOrFail';
interface Row {
id: number;
name: string;
}
const rows: Row[] = [
{ id: 1, name: 'first' },
{ id: 2, name: 'second' }
];
describe('findOrFail', () => {
it('returns the matching row', () => {
expect(findOrFail(rows, (r) => r.name === 'second', 'the second row')).toEqual({
id: 2,
name: 'second'
});
});
it('returns the first match when several qualify', () => {
expect(findOrFail(rows, () => true, 'anything').id).toBe(1);
});
// The whole point. Dereferencing a missed `.find()` gives "Cannot read
// properties of undefined" pointing at test plumbing; this says what was
// wanted and how many rows were searched.
it('throws naming what it looked for', () => {
expect(() => findOrFail(rows, (r) => r.name === 'absent', 'the absent row')).toThrow(
/the absent row/
);
});
it('reports how many rows it searched', () => {
expect(() => findOrFail(rows, () => false, 'nothing')).toThrow(/2 rows/);
});
it('says so when the collection was empty, which reads differently', () => {
expect(() => findOrFail([], () => true, 'anything')).toThrow(/0 rows/);
});
});