/marekkirejczyk-skills:refactor-testsfrom marekkirejczyk-skills
Refactors tests to follow type-specific rules, cover every case, and run at the fastest viable level. Use with a file, class, or PR to enforce test standards.
Ships in marekkirejczyk-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description, though model invocation is off: only you can start it.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Each test type has its own file next to this one: what the type checks, its rules, the cases it must cover, how its tests are named, and the shape of one. Find the type of each test in scope, read its file, and apply every rule in it.
| Test type | File |
|---|---|
| Browser tests (a real browser, the whole app) | browser.md |
| Database round trips (a repository's tests) | round-trips.md |
| Pure functions | pure-functions.md |
| Route handlers | route-handlers.md |
| Services | services.md |
Web component tests have no file here. Push down and the speed table apply to them, and those that stay are left as written.
Never add browser tests to a project that has none. No Playwright, no browser test files: write no browser test, add no Playwright dependency or config, and leave browser.md unread. A case only a browser could prove goes in the report as untested.
The scope is what you were asked to write or refactor tests for: a file, a class, or a branch's changes. You are done when all four hold:
it; a case with no test is a gap, whatever else passes.pure-functions.md has a verdict under Push down.Then pnpm format, pnpm lint and pnpm test pass, and the tests also pass shuffled (pnpm vitest run --sequence.shuffle <file> in the test's package) and one at a time. Browser tests run as Running in browser.md says.
Work in this order: the baseline from Speed, Push down, the type's rules and cases, then Speed's fixes. Each test then gets polished only once it has its final level.
Be aggressive: a test stays at a slow level only for what the table below says only that level can prove. The ladder, fastest first, is pure function, service, route handler or web component, browser. For each test in scope that isn't a pure-function test, try the fastest rung first and stop at the first that can prove what the test checks. Write a one-line verdict for it in your working notes: stays (what only this level proves), moves (to which level) or removed (the faster test that covers it).
| Level | Stays at this level | Moves down |
|---|---|---|
| Browser | One journey per critical flow, and what needs a real browser | Edge cases and logic, to route or component tests |
| Route handler | Each status code once, each outside service's wiring, each follow-up effect | Every further reason for a status |
| Service | Each outcome once, and what lands in the fakes | Decisions, to pure functions |
| Round trip | SQL behaviour, filter edges and locks | Row mapping written in TypeScript |
| Web component | Rendering and interaction | Formatting and state logic |
Extract to test. Pull out a small pure function when the code holds a real decision (a branch, a calculation, a parse) and the function gets a name from the domain. Move the code unchanged, under Refactoring, and keep it reachable through its module's real interface: a pass-through or a private helper made public only for a test stays where it is. When a test can only move by changing logic, its verdict is stays.
Remove duplicates. A test whose every assertion a faster or same-level test already proves is removed, slowest first. Search every test that exercises the code in scope. Change and delete only tests in scope, and report an out-of-scope duplicate with the faster test that covers it.
Proof. A test moves or goes only after the proof, one check per thing it asserted:
With no red test, the verdict is stays until a faster test goes red on that break.
Write one expect per thing a test checks, in the order a reader asks about them: what came back, then each effect. Each failure then names the one thing that broke. Compare each thing whole (toEqual on the whole value), and keep unrelated values out of a shared array:
expect(response.status).toBe(201);
expect(await response.json()).toEqual({
id: expect.stringMatching(UUID),
total: 1200,
});
expect(payments.charges).toEqual([{ amount: 1200, currency: "gbp" }]);
Write out every value the test can know: an error message, a URL built from an ID it has, a time it set. Match a value it can't know (a generated ID, a token, a session URL) by its shape: expect.stringMatching(/^[0-9a-f]{64}$/) for a 32-byte hex token, a UUID pattern for an ID. A shape fails on an empty string, a wrong format or a value in the wrong field, where expect.any(String) passes them all.
On a page, a browser test uses Playwright's auto-retrying assertions instead of toEqual on a value it has read (rule 3 of browser.md).
A refactor keeps the code's semantics: every caller sees the same answers, errors, writes and calls as before. Within that, change anything: tests and application code alike, interfaces as well as implementations (rename, move, split, merge, extract a pure function, change a signature and every caller with it).
Split big files as part of the refactor: above all, move helper functions out of the class or file that uses them into modules grouped by what they are about (an order's state changes in one, the text of its emails in another), each module with its own test file. Each module then has one subject, and its tests read as its list of cases. A split is a round like any other, priced before it is made (see Speed), because it multiplies whatever each test pays for.
Work in rounds: one change at a time, then pnpm lint and pnpm test, green before the next round. A test that goes red after a round means the round changed behaviour: undo it and take a smaller step.
A change of behaviour (a bug found, a case handled differently) is outside the refactor: report it, and leave the code as it behaves today, with a test that pins today's behaviour. Next to it, write the test for the right behaviour, red, and skip it (it.skip, Vitest's xit), named for the bug, so the fix only has to unskip it and delete the pin:
it("a negative amount (today: £-5)", () => {
expect(formatPounds(-500)).toEqual("£-5");
});
it.skip("a negative amount (bug: should read -£5)", () => {
expect(formatPounds(-500)).toEqual("-£5");
});
One example per test with fresh state multiplies whatever each test's setup costs. One suite grew from 825 tests to 2,200 this way, and its CI run went from 6 minutes to past its 20-minute limit, while elapsed time on a 16-core laptop barely moved. So price the work, and measure it.
A cost belongs at the highest level that can carry it.
| Level | Paid | Budget | What belongs here |
|---|---|---|---|
| Run | Once per vitest run | Seconds are fine | Compiling the app, running migrations once and saving a snapshot, building fixture templates, all in globalSetup |
| File | Once per test file | 100 ms, from the first import to the end of beforeAll | Importing the app, starting it, starting its database from the snapshot |
| Test | Every test, its own setup included | Its type's file; 100 ms for a test that needs fresh real state (a database or a subprocess of its own) | One request, one round trip, one call |
Vitest gives each test file a new process by default, so something kept in memory is gone by the next file: the run level hands its results on as files on disk.
Suite: the full suite's CPU time (user + system) rises by no more than 25% against the baseline, and fits CI's job limit if the project has one.
<skill-dir> is the folder holding this file.
Baseline first, on a clean checkout of the base commit, on a quiet machine. Record the suite's CPU time, its test count, and each test's and file's cost. Note the machine's load, and treat a loaded run as rough.
Suite: /usr/bin/time -p pnpm test, and add user and sys. Elapsed time hides extra work on a many-core machine, so the suite's check is CPU time.
Tests and files, by category: one table per package the scope touches, before and after. <tmp> is a temporary directory outside the repository.
<tmp>/map.json, giving each test file one category, for the files of both the before and the after state. Declare a category for each test type present, with its budget from its file, and for each other kind of test the package has (web components), with no budget_ms. Services (fakes) and Services (real) are separate categories, and a file mixing both takes the real one. A file left out shows as Unclassified.
{
"file_budget_ms": <the File level's budget>,
"categories": [{ "name": "Pure functions", "budget_ms": <its budget> }, { "name": "Web components" }],
"files": { "src/slug.test.ts": "Pure functions" }
}
pnpm vitest run --no-file-parallelism --reporter=<skill-dir>/scripts/speed-reporter.mjs > <tmp>/pass.log.<tmp>/rerun-1.log and so on, the same command with the files that node <skill-dir>/scripts/speed-table.mjs --map <tmp>/map.json --over <tmp>/pass.log lists. Take the median of each line for anything you report as over budget.node <skill-dir>/scripts/speed-table.mjs --map <tmp>/map.json <tmp>/pass.log <tmp>/rerun-*.log.Total and Time share include Setup: imports, setup files, beforeAll and afterAll. Avg per test and Over budget use each test's own time, beforeEach and afterEach included. Browser tests get a table of their own, measured with Playwright's reporter (Speed in browser.md). Tests any other runner owns are named in the report instead.
beforeAll, because tests also run shuffled and any of them can be first.timeout-minutes stay as they are.The first two items go in your final summary and in the PR description, the rest in your final summary. On the PR, add them as a ## Test speed section, or replace that section when it exists: gh pr edit on an open PR, and the description itself when you open one.
agents/openai.yaml (opens in a new tab)browser.md (opens in a new tab)pure-functions.md (opens in a new tab)round-trips.md (opens in a new tab)route-handlers.md (opens in a new tab)scripts/playwright-speed-reporter.mjs (opens in a new tab)scripts/speed-reporter.mjs (opens in a new tab)scripts/speed-table.mjs (opens in a new tab)services.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub marekkirejczyk/skills --plugin marekkirejczyk-skills11 skills in this plugin
/baby-sittingBaby-sit the current branch's pull request until it is merged. With nothing or manual it keeps the PR rebased, green and answered, working through the review bot's comments you pick, and leaves the merge to you; with auto it turns on GitHub auto-merge./dont-repeat-yourselfFind every concept implemented more than once in a scope (a repository, a PR, a directory, a concept) and give it one well-tested implementation. Identical copies are extracted automatically; behaviour differences and high-stakes code are brought to you for a decision at the end./eval-reportRun a repository's evals and turn the results into one static HTML report for fast review, with pass/fail counts, collapsible files, filters for failed and for new or changed cases, and links to the code. Run it with nothing for the whole repository, with commit or pr to mark what changed, or with only commit|pr to run just what changed./eval-report-install-ciSet up eval-report on GitHub Actions, so every pull request gets an HTML eval report linked at the top of its description and in one comment, built by a script without model tokens. Adds it to the existing eval job, or proposes a lean eval job of its own. Needs the eval-report skill installed next to it./explain-with-slidesBuild a slide deck that walks through a repository, the current one or one at a URL, (architecture and features) or through one pull request (its changes, before/after screenshots, diagrams, key files, diffs, tests and review risks), with screenshots taken from the code actually running. Published as a Claude Slides artifact or as a standalone HTML deck. Use only when the user asks for a repo presentation, onboarding deck or PR walkthrough deck./explain-with-videoMake a narrated explainer video of a repository or a pull request, as an MP4 with captions on a shareable video page. Scenes reveal and highlight item by item while a synthetic voice explains them (macOS say by default; OpenAI or ElevenLabs when configured). Use only when the user asks for a narrated video, screencast or video explainer of a repository, a feature or a pull request.Other plugins' skills for the same job