home / skills / wesleysmits / agent-skills / unit-test-generator

unit-test-generator skill

/.agent/skills/unit-test-generator

This skill generates unit tests for changed files and identifies coverage gaps to improve reliability and maintainability.

npx playbooks add skill wesleysmits/agent-skills --skill unit-test-generator

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
5.7 KB
---
name: generating-unit-tests
description: Generates unit tests for changed files and identifies coverage gaps. Use when the user asks to write tests, generate test suites, check coverage, or mentions Jest, Vitest, Playwright, or Mocha.
---

# Unit Test Generator & Validator

## When to use this skill

- User asks to generate tests for a file or function
- User wants to improve test coverage
- User mentions Jest, Vitest, Playwright, or Mocha
- User asks for edge case suggestions
- User wants to validate existing tests

## Workflow

- [ ] Identify changed or target files
- [ ] Detect test framework in project
- [ ] Analyze code to understand testable units
- [ ] Generate test file with appropriate structure
- [ ] Include edge cases and error scenarios
- [ ] Run tests to validate
- [ ] Check coverage if configured

## Instructions

### Step 1: Identify Target Files

For changed files:

```bash
git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(js|jsx|ts|tsx|vue)$' | grep -v '\.test\.|\.spec\.'
```

For specific file, derive test path:

- `src/utils/format.ts` → `src/utils/format.test.ts` or `__tests__/utils/format.test.ts`

### Step 2: Detect Test Framework

| Framework  | Config Files                               | Test Pattern         |
| ---------- | ------------------------------------------ | -------------------- |
| Jest       | `jest.config.*`, `package.json` (jest key) | `*.test.{js,ts,tsx}` |
| Vitest     | `vitest.config.*`, `vite.config.*`         | `*.test.{js,ts,tsx}` |
| Playwright | `playwright.config.*`                      | `*.spec.{js,ts}`     |
| Mocha      | `.mocharc.*`, `package.json` (mocha key)   | `*.test.{js,ts}`     |

Check installed framework:

```bash
npm ls jest vitest @playwright/test mocha 2>/dev/null
```

### Step 3: Analyze Source Code

Extract testable units:

- Exported functions and their signatures
- Class methods and their visibility
- React/Vue component props and behavior
- API handlers and their responses
- Pure functions vs side-effect functions

Identify inputs and outputs:

- Parameter types and valid ranges
- Return types and possible values
- Thrown errors and conditions
- External dependencies to mock

### Step 4: Generate Test Structure

**Jest/Vitest template:**

```typescript
import { describe, it, expect, vi } from "vitest"; // or from '@jest/globals'
import { functionName } from "../path/to/module";

describe("functionName", () => {
  it("should handle normal input", () => {
    expect(functionName(validInput)).toBe(expectedOutput);
  });

  it("should handle edge case", () => {
    expect(functionName(edgeInput)).toBe(edgeOutput);
  });

  it("should throw on invalid input", () => {
    expect(() => functionName(invalidInput)).toThrow(ExpectedError);
  });
});
```

**React component template:**

```typescript
import { render, screen, fireEvent } from '@testing-library/react';
import { Component } from '../Component';

describe('Component', () => {
  it('renders with default props', () => {
    render(<Component />);
    expect(screen.getByRole('button')).toBeInTheDocument();
  });

  it('handles user interaction', async () => {
    const onAction = vi.fn();
    render(<Component onAction={onAction} />);
    fireEvent.click(screen.getByRole('button'));
    expect(onAction).toHaveBeenCalledOnce();
  });
});
```

**Playwright E2E template:**

```typescript
import { test, expect } from "@playwright/test";

test.describe("Feature", () => {
  test("should complete user flow", async ({ page }) => {
    await page.goto("/path");
    await page.click('button[data-testid="action"]');
    await expect(page.locator(".result")).toBeVisible();
  });
});
```

### Step 5: Include Edge Cases

Always generate tests for:

- **Boundary values**: 0, -1, empty string, empty array, max values
- **Null/undefined handling**: missing optional params
- **Type coercion**: string numbers, boolean strings
- **Async behavior**: resolved, rejected, timeout
- **Error paths**: invalid input, network failures, permission denied

### Step 6: Run and Validate

Execute tests:

```bash
# Jest
npx jest --testPathPattern="<test-file>" --coverage

# Vitest
npx vitest run <test-file> --coverage

# Playwright
npx playwright test <test-file>

# Mocha
npx mocha <test-file>
```

### Step 7: Check Coverage

View coverage report:

```bash
# Jest/Vitest generate coverage in ./coverage/
open coverage/lcov-report/index.html
```

Coverage targets:

- Statements: 80%+
- Branches: 75%+
- Functions: 80%+
- Lines: 80%+

## Mocking Patterns

**Mock external modules:**

```typescript
vi.mock("../api", () => ({
  fetchData: vi.fn().mockResolvedValue({ data: "mocked" }),
}));
```

**Mock timers:**

```typescript
vi.useFakeTimers();
vi.advanceTimersByTime(1000);
vi.useRealTimers();
```

**Mock environment:**

```typescript
const originalEnv = process.env;
beforeEach(() => {
  process.env = { ...originalEnv, API_KEY: "test-key" };
});
afterEach(() => {
  process.env = originalEnv;
});
```

## Validation

Before completing:

- [ ] Tests pass without modification to source
- [ ] Edge cases covered
- [ ] Mocks properly reset between tests
- [ ] No flaky async behavior
- [ ] Coverage improved or maintained

## Error Handling

- **Framework not installed**: Run `npm install --save-dev <framework>`.
- **Tests fail immediately**: Check import paths and module resolution.
- **Mock not working**: Ensure mock is defined before import in Jest, or use `vi.mock` hoisting in Vitest.
- **Coverage not generating**: Add `--coverage` flag and check config for coverage settings.
- **Unsure about command**: Run `npx <framework> --help`.

## Resources

- [Jest Documentation](https://jestjs.io/docs/getting-started)
- [Vitest Documentation](https://vitest.dev/guide/)
- [Playwright Documentation](https://playwright.dev/docs/intro)
- [Testing Library](https://testing-library.com/docs/)

Overview

This skill generates unit and end-to-end tests for changed or specified files and identifies coverage gaps. It detects the project's test framework (Jest, Vitest, Playwright, Mocha), creates well-structured test files with edge cases and mocks, and can run tests and report coverage recommendations.

How this skill works

It inspects changed or target source files, extracts exported functions, classes, and component behavior, and infers inputs, outputs, and failure modes. It chooses the correct test template for the detected framework, inserts boundary and error-case tests, adds necessary mocks, and optionally runs the test runner to validate results and produce coverage reports.

When to use it

  • You want tests for a changed file or a specific function.
  • You need to increase or verify test coverage metrics.
  • The project uses Jest, Vitest, Playwright, or Mocha.
  • You want automated edge-case, error-path, and async behavior tests.
  • You need a baseline test suite to catch regressions quickly.

Best practices

  • Target only changed files or clear units to avoid noisy diffs.
  • Detect and respect the project's test framework and naming conventions.
  • Include boundary values, null/undefined, type-coercion, and async error cases.
  • Mock external modules, timers, and environment variables, and reset mocks between tests.
  • Run the test runner with --coverage and aim for recommended thresholds before merging.

Example use cases

  • Generate Jest unit tests for a modified utility function and add boundary/value checks.
  • Create Vitest tests for a React component with interaction tests using testing-library.
  • Produce Playwright E2E tests for a newly added user flow and validate visibility and navigation.
  • Identify coverage gaps after a feature change and generate additional tests to meet thresholds.
  • Add mocks for external API calls in a Node handler and test success and failure responses.

FAQ

Which frameworks are supported?

Jest, Vitest, Playwright, and Mocha are detected and supported with appropriate templates.

How are target files identified?

Changed files are discovered via git diff patterns or you can provide a specific path; test paths are derived using common project conventions.

Will the skill run tests and produce coverage?

Yes—if permitted, it will run the configured test runner and generate coverage reports using the framework's CLI flags.