💻

Test Generator

IntermediateDevelopment

Automatically generates comprehensive test cases for functions and components, covering edge cases and common scenarios.

testingjestvitestunit-testsautomation

Overview

The Test Generator skill creates comprehensive test suites for your code, covering happy paths, edge cases, and error scenarios.

SKILL.md Template

---
name: generate-tests
description: Generate comprehensive test cases for functions and components. Use when writing tests, improving coverage, or validating new code.
---

When generating tests, follow this methodology:

## 1. Analyze the Code
- Identify function inputs and outputs
- Map all code branches
- Find edge cases in logic
- Note external dependencies

## 2. Test Categories
Generate tests for each category:

### Happy Path
- Normal expected usage
- Typical input values
- Expected success scenarios

### Edge Cases
- Empty inputs (null, undefined, [], "")
- Boundary values (0, -1, MAX_INT)
- Single item collections
- Maximum length strings

### Error Scenarios
- Invalid input types
- Missing required fields
- Network failures (for async)
- Timeout conditions

### Integration Points
- Mock external services
- Verify API contracts
- Test event emissions

## 3. Test Structure
```typescript
describe('FunctionName', () => {
  describe('happy path', () => {
    it('should handle normal input', () => {});
  });

  describe('edge cases', () => {
    it('should handle empty input', () => {});
    it('should handle boundary values', () => {});
  });

  describe('error handling', () => {
    it('should throw on invalid input', () => {});
  });
});

4. Assertions

  • Use specific matchers (toBe, toEqual, toThrow)
  • Test both positive and negative cases
  • Verify side effects when applicable

## Example Output

For a `calculateDiscount` function:

```typescript
import { calculateDiscount } from './pricing';

describe('calculateDiscount', () => {
  describe('happy path', () => {
    it('applies percentage discount correctly', () => {
      expect(calculateDiscount(100, 10)).toBe(90);
    });

    it('handles decimal prices', () => {
      expect(calculateDiscount(99.99, 15)).toBeCloseTo(84.99);
    });
  });

  describe('edge cases', () => {
    it('returns original price for 0% discount', () => {
      expect(calculateDiscount(100, 0)).toBe(100);
    });

    it('returns 0 for 100% discount', () => {
      expect(calculateDiscount(100, 100)).toBe(0);
    });

    it('handles very small prices', () => {
      expect(calculateDiscount(0.01, 50)).toBeCloseTo(0.005);
    });
  });

  describe('error handling', () => {
    it('throws for negative price', () => {
      expect(() => calculateDiscount(-10, 10)).toThrow('Invalid price');
    });

    it('throws for discount over 100%', () => {
      expect(() => calculateDiscount(100, 150)).toThrow('Invalid discount');
    });
  });
});

Usage

/generate-tests src/utils/pricing.ts

Framework Support

This skill adapts to your testing framework:

  • Jest
  • Vitest
  • Mocha/Chai
  • React Testing Library
  • Playwright/Cypress

Add framework preference to your SKILL.md for consistent output.

Related Skills