If you are a web developer like me, you probably don’t enjoy writing tests.
Let’s be honest:
-
Writing code: fun.
-
Debugging: sometimes fun.
-
Writing test: pain, boring, and sometimes more painful than bug itself.
Recently, I found out something crazy:
AI can actually write around 70–80% of your unit tests and even E2E tests for you.
And the quality is not bad at all.
Sometimes even cleaner than mine (a little bit embarrassing).
How AI Writes Unit Tests
Let’s say you have a simple function:
export function sum(a: number, b: number) {
return a + b;
}
Normally, you need to create a test file, import Jest/Vitest helpers, think about test cases…
Very tiring.
But with AI (like ChatGPT, Claude, or Cursor), you just copy this function and ask:
“Generate unit tests for this function using Jest.”
Boom. You get something like:
import { sum } from './sum';
describe('sum', () => {
test('should return the total of two numbers', () => {
expect(sum(1, 2)).toBe(3);
});
test('should handle negative numbers', () => {
expect(sum(-2, 5)).toBe(3);
});
});
Clean, simple, ready to use.
Sometimes it even adds edge cases you don’t think about.
Writing Tests for Components (React)
When testing UI components, things get worse.
You need to mock props, mock API calls, fake events, check DOM…
But AI can read your component and build a full test setup.
For example, with a simple button component, I asked AI:
“Write a React Testing Library test for this component.”
And AI gave me:
-
render() setup
-
click event
-
expect() to check callback
-
even recommended using screen.getByRole()
E2E Tests With Playwright or Cypress
This part is even more surprising.
I gave AI the UI flow:
-
user login
-
click dashboard
-
open modal
-
submit form
And it produced a full Playwright script:
test('user can submit form', async ({ page }) => {
await page.goto('/login');
await page.fill('#username', 'test');
await page.fill('#password', '123456');
await page.click('button[type="submit"]');
await page.waitForURL('/dashboard');
await page.click('text=New Item');
await page.fill('#title', 'Hello');
await page.click('button[type="submit"]');
await expect(page.locator('.toast-success')).toBeVisible();
});
AI wrote it in 5 seconds.
So Is AI Perfect?
No.
AI is good, but not magic.
Sometimes:
-
It writes tests for cases your app will never do
-
It misunderstands your API
-
It uses selectors that don’t exist
But 70–80% of the time, the output is a very good starting point.
You can edit a little, adjust some parts, and done.
Still faster than writing from zero.
Should You Use AI for Tests?
If you:
-
hate writing tests
-
want better coverage
-
or simply want to look cool in your team
YES. 100%.
AI will not replace you, but it will replace your suffering.