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/); }); });