Generating Tests & Edge Cases

Prompt the model to generate meaningful tests, including the edge cases it would otherwise skip, in your framework and style.

TL;DR

  1. AI is strong at generating tests, especially the edge cases humans skip, if you ask for them by name.
  2. Specify the framework, the style, and that tests should assert behavior, not implementation.
  3. Review generated tests: a test that passes against buggy code is worse than no test.

Ask For Coverage

    Name The Categories

    Spell out the kinds of case you want so none are skipped.

    "Cover: happy path, empty, null,
    boundaries, invalid input, errors."
    Boundaries Explicitly

    Call out the edges where bugs hide, off-by-one, min/max, zero.

    "Test n = 0, n = 1, and n = max."
    Error Paths

    Ask for tests that assert the right errors are thrown.

    "Assert it throws on n < 1 with the
    exact message."

Match Your Suite

    Name The Framework

    State the test runner and version so syntax matches.

    "Vitest, TypeScript, describe/it,
    expect assertions."
    Template Test

    Paste one existing test and ask new ones to mirror it.

    "Match the structure and naming of
    this existing test: <paste>"
    Same Helpers

    Reuse your fixtures and factories instead of ad-hoc setup.

    "Use the existing makeUser() factory."

Behavior Over Internals

    Assert Outputs

    Check what the code returns or does, not how it does it.

    expect(total).toBe(1500)
    // not: expect(calc).toHaveBeenCalled()
    Avoid Over-Mocking

    Mock only true boundaries; over-mocking tests the mocks, not the code.

    Mock the network, not your own
    pure functions.
    Readable Names

    Ask for test names that describe the behavior under test.

    it('returns $0.00 for an empty cart')

Verify The Tests

    Check Assertions

    Confirm each expected value is actually correct, not just current.

    Is toBe(1500) the RIGHT answer,
    or just what the code outputs?
    Mutation Check

    Break the code on purpose; a good test should then fail.

    Introduce a bug -> test goes red?
    If not, the test is weak.
    Run Them

    Execute the suite; generated tests sometimes do not even run.

    npm test  # they pass AND are meaningful

Tips

  1. List the categories you want: happy path, empty, null, boundaries, errors, and concurrency where relevant.
  2. Give one existing test as a style template so new tests match your framework and assertions.

Warnings

  1. Models can write tests that assert whatever the current (possibly buggy) code does; verify the assertions are correct.
  2. Tests coupled to implementation details break on every refactor; ask for behavior-focused assertions.

In Practice

FAQ