Unit Tests Through Red-Green-Refactor

Build behavior-focused unit tests, deterministic fixtures, and boundary mocks through verified red-green-refactor cycles.

Development / Testingtddunit-testing
by AgentVoltv1.0.0
Free to sign up · every skill included with AgentVolt Pro

Before & after

Same prompt, same agent — the only difference is the skill.

Prompt“Write unit tests for our authentication module following TDD.”
Beforegeneric AI agent
Test-driven development means you write tests first, then write code to pass them. For an auth module, you'd think about the different scenarios: valid credentials should authenticate the user, invalid credentials should fail, expired tokens should be rejected, etc. You'd write test code that covers these scenarios. Then you write the actual implementation. Tests need to cover the happy path and error cases. You might also use mocks for external dependencies like a database. Good test coverage helps catch bugs early and makes refactoring safer.
Afterwith Unit Tests Through Red-Green-Refactor
I'll guide you through TDD for your auth module, from test definition through red-green-refactor cycles.

RED-GREEN-REFACTOR WORKFLOW
RED PHASE (Write failing test first)
Identify a single behavior: "authenticate should return a token for valid credentials"
Write a test that calls your auth function with known-valid inputs and asserts the output:
it("returns JWT token for valid username/password") {
  const token = authenticate("user@example.com", "password123");
  expect(token).toBeDefined();
  expect(token.startsWith("eyJ")).toBe(true);
}
Run test -> RED (fails because authenticate() doesn't exist yet).

GREEN PHASE (Write minimal code to pass)
Implement only what's needed to pass:
function authenticate(email, password) {
  return "eyJhbGciOiJIUzI1NiJ9...";
}
Run test -> GREEN (passes).

REFACTOR PHASE (Improve without breaking tests)
Strengthen the implementation: add credential validation, real JWT signing, error handling. Tests confirm each change doesn't break behavior.

TEST CASES FOR AUTH MODULE
Valid credentials: token returned, token contains email claim, token has expiry
Invalid password: AuthError thrown with "invalid credentials" message
User not found: AuthError thrown with "user not found" message
Expired token: verify() rejects with "token expired"
Malformed token: verify() rejects
Token revocation: revoked tokens don't authenticate

MOCKING STRATEGY
Mock database: pass a mock DB to auth() instead of real queries. Test returns correct error when DB fails.
Mock clock: test token expiry by advancing time (jest.useFakeTimers).

DELIVERABLE
Test file with 8-10 test cases covering happy path + all error scenarios. Implementation grows incrementally to pass each test.

About this skill

Your tests pass, but changing the behavior still breaks production. Or coverage is low and every new test seems to need another mock. This workflow turns requirements and uncovered branches into focused unit tests, then verifies that each test detects the intended defect before implementation changes.

What it does

  • Runs one behavior at a time through red, green, and refactor, separating real assertion failures from broken setup.
  • Selects tests at decision boundaries, including exact thresholds, rejected inputs, and dependency failures.
  • Builds fresh fixtures with small, explicit overrides instead of giant shared test objects.
  • Chooses real collaborators, fakes, stubs, or mocks according to the behavior under test.
  • Reads coverage gaps as missing behavior evidence and prioritizes consequential branches over percentage gains.
  • Adapts execution and isolation practices to Jest, Pytest, JUnit, Vitest, or Mocha.

What you get

Focused test code, the implementation changes needed to satisfy the requested behavior, and a concise verification record showing what failed, what passed, and what remains unverified. For review-only requests, you get specific test weaknesses and replacement cases without unnecessary production edits.

Best for

Developers adding a feature, reproducing a bug, covering legacy code, or untangling brittle tests in an existing project.

SKILL.md preview

SKILL.md
---
name: unit-tests-through-red-green-refactor
description: Builds behavior-focused unit tests, deterministic fixtures, and boundary mocks, prioritizing coverage gaps through one verified red-green-refactor cycle at a time. Adapts to Jest, Pytest, JUnit, Vitest, and Mocha. Use when the user says "TDD this feature", "write unit tests for this", "reproduce this bug with a test", "my tests pass but the bug is still there", "these mocks keep breaking", "improve coverage on this module", "generate fixtures for these tests", or "review my test suite".
---

# Unit Tests Through Red-Green-Refactor

… (sign up to view the full skill)
Sign up to view, copy, and install the full skill

More development skills

View all Development skills →