home / skills / git-fg / thecattoolkit / generating-tests

This skill generates Vitest tests from documentation code examples, ensuring accuracy and coverage while aligning with project conventions.

npx playbooks add skill git-fg/thecattoolkit --skill generating-tests

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

Files (6)
SKILL.md
3.8 KB
---
name: generating-tests
description: "Follows project conventions, extracts examples from documentation, converts them to tests, and ensures documentation accuracy through automated testing. PROACTIVELY Use when generating comprehensive Vitest tests for code examples in JavaScript concept documentation pages. Do not use for end-to-end browser automation, user flow testing, or browser-server integration → see testing-e2e skill."
user-invocable: true
allowed-tools: [Read, Write, Bash, Grep, Glob]
---

# Test Writer for Documentation Code Examples

Extracts, converts, and verifies documentation examples.



## When to Use This Skill

Use this skill when you need to:
- Generate tests for new concept documentation pages
- Add tests when updating existing documentation examples
- Verify documentation accuracy through automated testing
- Ensure all code examples in documentation work as documented
- Create comprehensive test coverage for educational content

## Test Writing Methodology

Follow a four-phase approach to create comprehensive tests:

1. **Extract & Categorize** - Scan documentation for code examples and categorize by type
2. **Determine Structure** - Organize tests by project conventions
3. **Convert to Tests** - Transform examples into proper Vitest tests
4. **Handle Special Cases** - Address DOM, async, error, and edge cases

**See:** `references/methodology.md` for complete four-phase methodology

## Quick Start Pattern

### Basic Transformation

```javascript
// Documentation
console.log('Hello, World!') // Hello, World!

// Test
it('should produce expected output - Line 1', () => {
  const result = /* code from documentation */
  expect(result).toBe('Hello, World!')
})
```

### Error Testing

```javascript
// Test error cases
it('should throw error - Line X', () => {
  expect(() => problematicFunction()).toThrow('Expected error')
})
```

### Async Testing

```javascript
// Test async operations
it('should handle async - Line X', async () => {
  const result = await asyncFunction()
  expect(result).toEqual(/* expected */)
})
```

**See:** `references/examples.md` for complete worked examples

## Test Conventions

### Import Pattern
```javascript
import { describe, it, expect } from 'vitest'
```

### File Naming
- Standard tests: `{concept-name}.test.js`
- DOM tests: `{concept-name}.dom.test.js`

### Test Structure
```javascript
describe('{Concept Name}', () => {
  it('should {expected behavior} - Line {line}', () => {
    // Implementation
  })
})
```

**See:** `references/conventions.md` for complete conventions and validation checklist

## Common Patterns

- **Output Verification**: Assert expected results
- **Side Effect Testing**: Test mutations and state changes
- **Error Handling**: Test error conditions
- **Async Operations**: Handle promises and async/await
- **DOM Testing**: Use jsdom environment for browser APIs

**See:** `references/patterns.md` for detailed pattern examples

## DOM Testing

For DOM-related tests:

```javascript
/**
 * @vitest-environment jsdom
 */
import { vi } from 'vitest'

beforeEach(() => {
  document.body.innerHTML = '<div id="app"></div>'
})
```

**See:** `references/dom-testing.md` for complete DOM testing guide

## Running Tests

```bash
# Run all tests
vitest

# Run specific file
vitest run {concept-name}.test.js

# Run with coverage
vitest run --coverage
```

## Reference Materials

**Core Implementation:**
- `references/methodology.md` - Four-phase test writing methodology
- `references/conventions.md` - Project conventions and validation checklist
- `references/examples.md` - Complete worked examples
- `references/patterns.md` - Common test patterns and examples
- `references/dom-testing.md` - DOM testing setup and patterns

**Quality Standards:**
Each test must be traceable, use appropriate assertions, follow naming conventions, include clear descriptions, handle edge cases, and pass when executed.

Overview

This skill extracts code examples from JavaScript concept documentation, converts them into Vitest tests, and validates documentation accuracy through automated test runs. It follows project conventions for imports, file naming, and test structure to produce traceable, maintainable tests. Use it proactively when creating or updating educational docs to keep examples working and documented behavior verified.

How this skill works

The skill scans documentation pages for code blocks and categorizes examples (sync, async, error, DOM). It maps each example to the project test structure, generates Vitest test cases with clear descriptions and line references, and handles special cases like async awaits, thrown errors, and DOM setup. The output follows import and filename conventions and is ready to run with standard Vitest commands.

When to use it

  • Generating comprehensive Vitest tests for new concept documentation pages
  • Adding tests when documentation examples are updated
  • Verifying that all code snippets in docs run as shown
  • Creating repeatable checks to prevent documentation regressions
  • Converting example outputs and side effects into automated assertions

Best practices

  • Follow the four-phase approach: Extract & Categorize, Determine Structure, Convert to Tests, Handle Special Cases
  • Use descriptive test and file names that match project conventions for traceability
  • Prefer explicit assertions for outputs and side effects; test error messages and edge cases
  • Use jsdom environment for DOM examples and reset DOM state in beforeEach
  • Keep tests small and focused: one behavior per it() block with a line reference

Example use cases

  • Transform a readme code block printing output into a vitest it() with expect checks
  • Convert an async example into an await-based test that asserts resolved values
  • Create error-case tests for examples that show thrown exceptions or invalid inputs
  • Generate DOM tests with @vitest-environment jsdom and setup/teardown for interactive examples
  • Batch-generate tests for a documentation section to run as part of CI

FAQ

Will this generate tests for end-to-end browser flows?

No. This skill targets unit-style Vitest tests for documentation examples. For end-to-end browser automation or full user flows, use a dedicated E2E testing approach.

How are DOM examples handled?

DOM code blocks become jsdom tests. The skill adds the vitest jsdom environment directive, sets up document body in beforeEach, and uses isolated DOM state per test.