// Generating rows programmatically — when the table itself comes from data
import { describe, test, expect } from 'vitest';
import { isValidEmail } from './validation';
const validEmails = [
'simple@example.com',
'user.name+tag@example.co.uk',
'a@b.io',
];
const invalidEmails = [
'',
'no-at-sign',
'@no-local-part.com',
'no-domain@',
'spaces in@email.com',
];
describe('isValidEmail', () => {
test.each(validEmails)('accepts "%s"', (email) => {
expect(isValidEmail(email)).toBe(true);
});
test.each(invalidEmails)('rejects "%s"', (email) => {
expect(isValidEmail(email)).toBe(false);
});
});
describe.each — 같은 suite, 여러 타겟·typescript
// describe.each — when whole describe blocks differ by parameter
import { describe, test, expect } from 'vitest';
import { createReducer } from './reducer';
// Run the same suite for every reducer variant.
describe.each([
['counter', createReducer('counter'), 0],
['toggle', createReducer('toggle'), false],
['list', createReducer('list'), []],
])('%s reducer', (name, reducer, initial) => {
test('returns the initial state for an unknown action', () => {
expect(reducer(initial as any, { type: 'UNKNOWN' })).toEqual(initial);
});
test('throws on a null action', () => {
expect(() => reducer(initial as any, null as any)).toThrow();
});
});
Track 2 assertion lesson 의 divide 테스트를 단일 test.each 테이블로 다시 짜. Happy path (10/2, 7/2, 0/10) 와 throw 케이스 (5/0 — expected 가 'throws' 문자열인 row 써서 본문에서 switch, OR throw row 만 위한 별개 test.each 써). 원본과 가독성과 라인 수 비교.
Hint
Throw 케이스 다르게 처리하려고 본문에서 분기하면, 신호야 — happy path 용 parametrized 테스트 하나와 에러 케이스용 하나로 쪼개.
Progress
Progress is local-only — sign in to sync across devices.