top of page

Use AI for Tests — AI-Assisted Coding, Part 6

  • Contributor
  • 6 days ago
  • 3 min read

Updated: 4 hours ago

AI-Assisted Coding · Part 6

Generating tests with AI saves time. But generated tests can be fake-rigorous. Review carefully.

Step 1: Generate Tests for Existing Code (10 min)

"Write Jest tests for this function: [paste function] Cover: - Happy path - Empty input - Boundary values - Error cases"

AI returns tests. Review them.

Often: produces decent coverage in minutes.

Step 2: Verify the Tests Are Real (10 min)

Look for fake tests:

test("should work", () => { expect(true).toBe(true); });

Or:

test("calls myFunc", () => { myFunc(); // no assertion! });

Read every assertion. Does it actually verify behavior?

Step 3: Watch for Tautological Tests (10 min)

test("formatPrice returns formatted price", () => { const result = formatPrice(100); expect(result).toBe(formatPrice(100)); // ← tests itself });

Or:

test("...", () => { expect(complexFunction(input)).toBe(complexFunction(input)); });

Self-referential. Useless.

AI sometimes does this when it doesn't know the expected output.

Step 4: TDD-Style With AI (15 min)

Reverse:

"Generate Jest tests for a function called `parseDate` that: - Takes a string - Returns a Date - Throws on invalid input Output only the tests."

AI writes tests first. You then implement to make them pass.

Powerful: tests define the spec; you implement against it.

Step 5: Edge Cases Especially (10 min)

"Generate edge case tests for this function. What inputs might break it?"

AI often spots cases you missed:

  • Very large inputs

  • Empty

  • Concurrency

  • Unicode

  • Boundary values

Trust but verify. Some are real edges; some are nonsense.

Step 6: Avoid Over-Mocking (15 min)

AI loves to mock everything:

jest.mock("../db"); jest.mock("../email"); jest.mock("../analytics");

Result: tests that pass when nothing works.

Mock only at:

  • True boundary (network, time, randomness)

  • External services

Internal code: use real implementations when possible.

Step 7: Integration Tests Help More (10 min)

For features: integration tests (real DB, real HTTP) > 100 unit tests with mocks.

"Generate Cypress E2E tests for this user signup flow."

AI writes the click-through. Real browser; real DB. Verifies the actual experience.

Step 8: Test Data Generation (10 min)

"Generate 20 realistic User objects for testing. Vary names, countries, ages. Include edge cases (very long names, accents, emojis)."

AI creates fixtures. Useful.

Better than:

const user = { name: "test", age: 1 };

That tells you the code works for test. Not for real users.

Step 9: Coverage Reports as Guide (10 min)

npm test -- --coverage

Find untested lines. Feed them to AI:

"This function is untested. Write tests."

Targeted gap-filling.

But: 100% coverage ≠ good tests. Quality matters more than line count.

Step 10: Trust Your Tests (10 min)

After AI generates:

  • Run them all

  • Look at failure messages (good error messages?)

  • Run them against a buggy version (do they catch it?)

  • Mutation testing if available

If AI's tests pass against broken code: they're not real tests.

What You Just Did

AI for tests: generate, verify real, no tautology, TDD-style, edge cases, no over-mock, integration, fixtures, coverage gaps, trust. Good test generation.

Common Failure Modes

Accept fake tests. "Tests pass" but verify nothing.

Over-mock. Tests don't reflect reality.

100% coverage with garbage tests. False security.

No mutation / negative testing. Don't know if tests catch bugs.

Generate; never read. Garbage tests committed.

Continue the AI-Assisted Coding path

Part of the AI-Assisted Coding learning path.

bottom of page