Plan tests for AI-generated features
Map each requirement to unit, integration or end-to-end checks, including invalid input, empty states, authorisation and regression cases.
By Invicly Tech, , in Prompt Engineering and Vibe Coding. About 9 minutes to read.
In short
Generated code needs a test plan, because it can look right and still fail on odd input. Map each requirement to a unit, integration or end-to-end test, then add cases for invalid input, empty states and who is allowed to do what. Every bug you fix gets one regression test.
Think of it like this: It is like checking a new door: you try the key, a wrong key, a locked state and a stranger, not just the one happy walk-through.
Explained simply
Code from an assistant often works for the example it was shown and breaks on the next one. Tests are how you find out. A test strategy decides which tests to write and where, so you spend effort on the risks that matter.
Start from the requirements. For each one, ask: what is the cheapest test that would prove it? A rule about rounding is a unit test. A route that validates input and saves data is an integration test. A user signing in and buying something is an end-to-end test.
Then add the cases people forget. What if the input is empty, too long or the wrong type? What does the page show when there is no data yet? What happens when a signed-out user, or a user with the wrong role, calls the action?
Every time you fix a bug, add a regression test first and watch it fail, then fix the code and watch it pass. In a Next.js project, Vitest is a good runner for unit and component tests.
Press Play on a diagram to walk through it one step at a time.
- 1End-to-end
A real browser walks a full user flow; few, slow, high value
- 2Integration
Several parts together, such as a route handler with its validation
- 3Unit
One function or component alone; many, fast, precise
- Happy path
- Item appears in cart
- Total updates
- Invalid input
- Quantity 0
- Quantity abc
- Quantity 10000
- Empty states
- Empty cart message
- Authorisation
- Signed out is refused
- Other user's cart hidden
- Regression
- Duplicate click adds once
In depth
Map requirements to levels
Write a small table: requirement on the left, test level on the right. Unit tests are fast and precise, so use them for logic. Integration tests check that parts agree with each other, such as a handler and its validation. End-to-end tests are slow, so keep them for the few flows that must never break.
The table also shows gaps. A requirement with no row has no proof. A row with three levels may be over-tested.
- Pure functions and rules
- Runs in milliseconds
- Pinpoints the failing line
- Many of them
- Handler plus validation
- Component plus hook
- Catches wiring mistakes
- Some of them
- Real browser, real flow
- Slow and broader
- Proves the user outcome
- A handful
Invalid input and empty states
Assistants tend to cover the happy path they demonstrated. Ask yourself for each input: empty, missing, too long, wrong type, special characters, and the biggest allowed value. Add a test for the ones that can happen.
Empty states are the screens with no data: a new account, an empty search result, a failed fetch. They are real screens that real users see first, so give each one a test that checks the message or call to action.
- Empty and missing values
- Too long and too large
- Wrong type or format
- No results yet
- Request fails
Authorisation boundaries
Authorisation decides who may do what. Test it on the server side, where the rule lives. Hiding a button in the page is not protection, because anyone can call the route directly.
Write at least three cases for each protected action: no user, a user without permission, and a user with permission. Also check that a user cannot reach another user's data by changing an id in the request.
- Test to Route handler: Request with no session1Request with no session
- Route handler to Auth check: Who is this?2Who is this?
- Auth check to Route handler: Nobody3Nobody
- Route handler to Test: 401, no data returned4401, no data returned
- Test to Route handler: Request as a different user's id5Request as a different user's id
- Route handler to Test: 403 or 404, other user data hidden6403 or 404, other user data hidden
Regression tests and Vitest in Next.js
A regression test is a test for a bug you already hit. Write it so it fails on the broken code. Then fix the bug and see it pass. The test stays forever as a guard.
To set up Vitest in Next.js, the official guide installs vitest, @vitejs/plugin-react, jsdom, @testing-library/react, @testing-library/dom and vite-tsconfig-paths as dev dependencies, adds a vitest.config file and a test script. Vitest does not support async Server Components, so cover those with end-to-end tests.
- Fail first, then fix
- One regression test per bug
- Install Vitest as dev dependencies
- Async Server Components need end-to-end tests
Learn it in 5 short steps
About 35m each. Track them with a timer in your plan.
- 1Map requirements to test levels~35m
- 2Cover invalid input and empty states~35m
- 3Test authorisation boundaries~35m
- 4Add a regression test per bug~35m
- 5Set up Vitest in Next.js~35m
Key points
- Write a table of requirements and the level that proves each one.
- Cover invalid input, empty states and error paths, not only the happy path.
- Test authorisation: a signed-out or wrong-role user must be refused.
- Add one regression test for each bug, written so it fails before the fix.
- Vitest with React Testing Library covers unit and component tests in Next.js. Async Server Components are better tested end to end.
Key terms
Tap a card to flip it. 0/5 known
Examples
import { describe, it, expect } from "vitest";
import { parseQuantity } from "./parseQuantity";
describe("parseQuantity", () => {
it("parses a valid number", () => {
expect(parseQuantity("3")).toBe(3);
});
it("rejects text and empty input", () => {
expect(parseQuantity("abc")).toBeNull();
expect(parseQuantity("")).toBeNull();
});
});A Vitest unit test with a bad-input case.
A requirements table and its tests
Requirement: quantity must be a whole number from 1 to 99. Step 1: pick the unit level, since it is a pure rule. Step 2: write a happy case and three bad cases. Step 3: run the tests against the assistant's code. Step 4: if "0" passes validation, you have found a bug, so keep that test as a regression guard. Step 5: record the table in the pull request so the reviewer sees the coverage.
import { describe, it, expect } from "vitest";
import { parseQuantity } from "./parseQuantity";
describe("parseQuantity", () => {
it("accepts 1 to 99", () => {
expect(parseQuantity("1")).toBe(1);
expect(parseQuantity("99")).toBe(99);
});
it.each(["0", "100", "abc", "", "2.5"])("rejects %s", (v) => {
expect(parseQuantity(v)).toBeNull();
});
});Setting up Vitest in a practice repo
Prerequisites: a Next.js project and Node installed. Step 1: install the dev dependencies. Step 2: create vitest.config.mts with the React plugin, the tsconfig paths plugin and the jsdom environment. Step 3: add a test script to package.json. Step 4: write one tiny test and run it. Evidence: the runner prints one passing test. Rollback: npm uninstall the packages and delete the config file.
npm install -D vitest @vitejs/plugin-react jsdom \
@testing-library/react @testing-library/dom vite-tsconfig-paths
# add to package.json scripts: "test": "vitest"
# vitest.config.mts: plugins [tsconfigPaths(), react()],
# test: { environment: "jsdom" }
npm test -- --runQuick reference
- Table each requirement against the cheapest level that proves it.
- Unit for logic, integration for wiring, end-to-end for the few critical flows.
- Test invalid input: empty, missing, too long, wrong type, boundary values.
- Test empty states: no data yet, no results, failed fetch.
- Test authorisation on the server: no user, wrong user, right user.
- One regression test per bug, written to fail before the fix.
- Vitest does not run async Server Components. Use end-to-end tests for them.
Common mistakes
- Asking the assistant to write tests from its own code, so the tests copy its mistakes.
- Testing only the happy path, because that is what the assistant demoed.
- Skipping the authorisation test because the UI hides the button.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
List five invalid inputs to test for a field that accepts an age from 0 to 120.
- Exercise 2medium
Make a requirements table for a notes feature: create, list, delete. Choose a level for each and add one authorisation case.
- Exercise 3hard
Write a regression test for this bug: submitting the form twice quickly creates two notes. Describe how you prove it fails first.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Should the assistant write the tests?
- It can draft them, but you decide the cases from the requirements. Tests written only from its own code tend to repeat its mistakes.
- How many tests are enough?
- Enough that every requirement and every risky case has a check. More tests that repeat the same proof add cost without safety.
- What is the difference between Vitest and Playwright?
- Vitest runs fast unit and component tests in Node. Playwright drives a real browser for end-to-end flows. Most projects use both.