DEV Community

Leena Malhotra
Leena Malhotra

Posted on

JavaScript Unit Tests, Auto-Written: What Broke & What Worked

Writing unit tests isn’t the hard part.

Maintaining them is.
Especially when your app shifts every two weeks and the only thing growing faster than the codebase is the tech debt nobody wants to touch.

So like every dev looking for a smarter way out, I asked:
Can AI write my JavaScript unit tests for me—without wasting more time than it saves?

Here’s what happened when I tried.
What broke. What worked. And where AI fits in the real-world testing flow.

The Stack

I tested this in two environments:

A vanilla JS project with Jest

A React app using Testing Library

I used Crompt AI as the interface—specifically the Code Explainer and AI Companion, both of which support multi-model prompts (GPT-4, Claude, Gemini, Grok).

The input: functional code blocks
The goal: generate unit-level tests—not e2e, not mocks for APIs, just coverage for isolated logic.

What Worked Surprisingly Well

✅** Fast Coverage of Pure Functions**
If you give GPT-4 or Claude a clean function like:

Useful? Extremely—especially when onboarding onto legacy code or backfilling tests.

React Component Testing (With Context)
When I pasted a React component with its props and default state, the Longform Editor plus Claude could write decent tests using @testing-library/react.

It even inferred:

What props were required

Expected visual/textual output

Async behavior (like useEffect firing on mount)

This saved ~30–40% of test writing time per component—especially for forms and UI logic.

Refactoring + Tests Together
The real power showed when I used the Code Explainer to first refactor a messy function, then ask for test coverage.

Cleaner input → better output. Obvious in theory. Crucial in practice.

What Broke (And Why)

Complex State Logic

For functions with multiple state mutations or reducer-style flow, AI often missed critical test branches.

Example: A checkout flow with coupon logic + inventory edge cases.
AI wrote happy path tests.
But no test for when coupon.expired === true && cart.total === 0.

Fix: I had to nudge it with manual prompts:
“Write a test for when the coupon is expired and cart is empty.”
Still saved time—but not hands-off.

Async/Await Hell

Even with clear async functions, generated tests sometimes forgot await in expect statements—or skipped done() callbacks entirely when mocking fetch.

You’d get:

js
Copy
Edit
expect(data).toBeDefined();
Instead of:

js
Copy
Edit
await expect(getData()).resolves.toBeDefined();
Fix: GPT-4 handled this better than Gemini, especially with context from prior code.

No Awareness of Testing Style

This one stung.

Some teams use describe/it, others use test, some prefer jest.fn(), others use vi.fn() with Vitest.

The AI didn’t know unless I told it. Default outputs were Jest-flavored, even if my original repo used Vitest + TS.

Fix: I now prompt with framework preference first, or include a test snippet as baseline style.

Pro Tips (So You Don’t Waste Time Like I Did)

Paste existing tests first. Let the AI learn your style.

Chunk your logic. The smaller the function, the smarter the test.

Use the AI Companion for iterative tweaks. Once a test is generated, ask it:

“What edge cases are missing?”

“How would you test failure states?”

Use Claude for critical logic, GPT-4 for speed. Claude reasons better, but GPT-4 gets more done per token.

Never trust without reading. These aren’t prod-ready out of the box.

My Verdict (From a Dev Who Actually Deploys)

Auto-writing unit tests with AI won’t replace your QA process.
But it will:

Accelerate onboarding

Unblock boring coverage tasks

Help you spot logic gaps

Reduce mental friction before PR

In my flow, I now treat Crompt AI as a test co-pilot, not a full generator.
It drafts. I decide.

The sweet spot?
Backfilling logic tests, scaffolding new features, and documenting assumptions through test intent.

Final Thought: Don’t Ship Blind. Ship With AI-Verified Confidence.

You don’t write tests just to check boxes.
You write them to encode how your system thinks.

And AI can help externalize that thinking—faster, clearer, and more thoroughly than your own brain after 3 hours of debugging.

So no, it’s not perfect.
But yes—it’s good enough to change how we build.

And the best part?

You’ll never write the same “adds two numbers” test again.

-Leena:)

Top comments (0)