From 532 Test Runs to 20 Unit Tests: Right-Sizing a Schema Test Suite
This blog validates every post's frontmatter with a Zod schema: 224 lines in src/lib/schemas/frontmatter.ts that check required fields, a 160-character description limit, the date format, and a fixed tag list. When the schema went in yesterday, its tests went into the Playwright end-to-end suite as a 1,145-line spec file. This morning I moved them to Vitest.
The short answer: the schema never needed a browser test runner, and running it in one multiplied every test by seven. The replacement is 20 unit tests in 246 lines that finish in under half a second. There are still a few gaps, listed at the end.
Where the count came from
The Playwright config has seven projects: desktop Chrome, Firefox and Safari (WebKit), Pixel 5, iPhone 13, iPad Pro, and a wide desktop Chrome viewport. Every test under tests/e2e runs once per project.
The frontmatter spec had 76 tests, so it produced 532 runs. The whole suite was 735: those 532 plus a 29-test migration spec, also run seven times. Playwright will count them for you:
npx playwright test tests/e2e/frontmatter-validation.spec.ts --list | tail -1
# Total: 532 tests in 1 file
npx playwright test tests/e2e/frontmatter-validation.spec.ts --list --project=chromium | tail -1
# Total: 76 tests in 1 filePure functions in a browser runner
Only 4 of the 76 tests called page.goto(). The rest called the schema's parseFrontmatter() with an object and checked whether it threw:
test('Missing title throws validation error', () => {
const invalidFrontmatter = {
description: 'Test description',
date: '2024-11-10',
tags: ['typescript'],
}
expect(() => parseFrontmatter(invalidFrontmatter, 'test.mdx')).toThrow(/title/i)
})That test never opens a page. Run under seven browser projects, it executes the same function in Node seven times and reports seven results. Seven identical passes add no coverage, and when the schema changes, seven identical failures add noise. The suite also starts the dev server before any of it runs, because the real E2E tests need it.
I didn't time the old spec on its own before deleting it, so I don't have a fair before-and-after speed comparison. The run count was the problem I could see.
The rule
The split I'm using now is mechanical:
- If the test calls a function directly, it's a Vitest unit test.
- If it needs
page.goto(), a rendered DOM, or a browser API, it's a Playwright test.
Vitest 4 picks up tests/unit/** with a jsdom environment and the same @/ path alias as the app. The schema tests read like this:
it('should reject description over 160 characters', () => {
const data = {
title: 'Test Post',
description: 'a'.repeat(161),
date: '2025-01-01',
tags: ['javascript'],
}
expect(() => FrontmatterSchema.parse(data)).toThrow()
})Running the unit suite:
npx vitest run
# Test Files 2 passed (2)
# Tests 23 passed (23)
# Duration 386ms (tests 10ms)That's 20 schema tests plus 3 in an example file left from the Vitest setup. Most of the 386 ms is starting jsdom; the tests themselves take 10 ms.
What the 20 tests cover, and what they dropped
| Group | Tests |
|---|---|
| Required fields (title, description, date, tags) | 4 |
| Tag taxonomy: allowed, rejected, maximum of 10 | 3 |
| Description length at 160 and 161 characters | 2 |
| Date format, valid and invalid | 2 |
| Optional field defaults (draft, islands, hero image) | 3 |
| Invalid data (empty title, long title, unknown island) | 3 |
| Schema structure | 3 |
The old file had more: a loop that accepted each allowed tag one at a time, hero image edge cases, fields reserved for later phases, and a regression group that parsed every real post. I dropped the per-tag loop because one valid and one invalid tag test the rule; the rest is Zod's enum doing what enum does. The real-post regression is covered by the build itself. Every post goes through parseFrontmatter() during next build, and an invalid one stops the build with the field and file name.
Gaps
- Structure tests. Two of the three "Schema Structure" tests assert that
FrontmatterSchema.shape.titleand friends are defined. That checks Zod's object shape rather than a rule I wrote, so they're next to go. - Placeholder E2E tests. The new
frontmatter-integration.spec.tshas 7 tests (build fails on invalid frontmatter, drafts hidden in production, and so on), and all 7 aretest.skip. The reporter shows them as skipped, which is accurate, but they cover nothing yet. - Impossible dates. The schema checks dates with
new Date(value + 'T00:00:00Z'), and V8 rolls February 30th over to March 1st, so2024-02-30passes. The date check needs to compare the parsed date back to the input, with a test for that case.
Ratio as a smell
Lines of test per line of code went from about 5.1 (1,145 over 224) to about 1.1 (246 over 224). I use the ratio as a prompt to go and read the tests. Five lines of test per line of schema meant the tests were enumerating the library's behaviour instead of the rules I wrote.
So what?
A checklist I'd run on any test before it goes into an E2E suite:
| Question | If yes |
|---|---|
| Does it call a function directly? | Unit test |
| Does it need a rendered page, navigation, or a browser API? | E2E test |
| Will it give the same result in every browser project? | Restrict it to one project, or move it out of Playwright |
| Does it assert on the library's structure rather than your rule? | Delete it |
| Does the build already enforce it? | Don't duplicate it |
| Is it skipped? | Track it as missing coverage, not as a test |
Next: delete the structure tests, tighten the date check, and decide whether a text blog needs seven browser projects for the E2E tests that remain.