unit-test-generator
---
What this skill does
---
name: unit-test-generator
description: Intelligent unit test generator for frontend projects that detects existing test frameworks and generates comprehensive tests for functions, components, and modules. Use when you need to: (1) Add unit tests to existing codebases, (2) Set up testing infrastructure for new projects, (3) Generate test cases for specific functions or components, (4) Ensure architectural consistency in testing approach across projects. Supports Jest, Vitest, Mocha with React, Vue, Angular frameworks.
---
# Frontend Unit Test Generator
An intelligent assistant for creating and managing unit tests in frontend projects. This skill automatically detects your project's testing setup and generates appropriate, framework-consistent test code.
## Quick Start
### Installation
```bash
npm install
```
### Basic Usage
Generate tests for any file or component:
```bash
node scripts/detect-test-framework.js /path/to/project
node scripts/generate-test.js src/components/Button.js
```
## Core Capabilities
### Framework Detection
Automatically identifies your project's testing stack:
- **Test Frameworks**: Jest, Vitest, Mocha, Jasmine
- **Assertion Libraries**: Expect, Chai, Should
- **Testing Tools**: Testing Library, Sinon, Cypress
- **Configuration Files**: jest.config.js, vitest.config.ts, mocha.opts
- **Test Structure**: __tests__/, test/, tests/, *.test.js patterns
### Smart Test Generation
- **Component Tests**: React, Vue, Angular with Testing Library
- **Function Tests**: Pure functions, async functions, API calls
- **Mock Generation**: API mocks, module mocks, time mocks
- **Edge Cases**: Null/undefined handling, error scenarios
- **Coverage Analysis**: Identifies untested code paths
### Framework Consistency
Maintains your project's existing testing patterns:
- Same assertion style (expect vs assert)
- Consistent file organization
- Compatible test descriptions
- Proper mock patterns
## Usage Examples
### 1. Detect Project Testing Setup
```javascript
const TestFrameworkDetector = require('./scripts/detect-test-framework');
const detector = new TestFrameworkDetector('./my-project');
const analysis = detector.detect();
console.log(analysis);
// Output: Frameworks, utilities, config files, recommendations
```
### 2. Generate Test for Component
```javascript
const TestGenerator = require('./scripts/generate-test');
const generator = new TestGenerator('./my-project', {
framework: 'jest',
react: true
});
const result = generator.generateForFile('src/components/Button.jsx');
// Returns: { testPath, testContent, analysis }
```
### 3. Setup Testing for New Project
```javascript
const TestConfigGenerator = require('./scripts/setup-test-config');
const config = new TestConfigGenerator('./my-project', 'jest', {
react: true,
typescript: true,
coverage: true
});
const files = config.writeConfigs(); // Creates config files
const deps = config.getDependencies(); // Returns required packages
```
## Interactive Test Creation
### For Existing Projects
1. **Analyze Project**: Run framework detection first
2. **Select File**: Choose file/component to test
3. **Generate Tests**: Auto-generate consistent test code
4. **Review & Customize**: Add specific test cases
5. **Run Tests**: Verify tests pass
### For New Projects
1. **Framework Selection**: Choose based on project needs
- See [framework-selection.md](references/framework-selection.md)
2. **Setup Configuration**: Generate config files
3. **Install Dependencies**: Add required packages
4. **Create First Tests**: Start with critical components
## Generated Test Patterns
### React Component Tests
```javascript
describe('Button', () => {
it('renders without crashing', () => {
render(<Button />);
expect(screen.getByRole('button')).toBeInTheDocument();
});
it('handles click events', async () => {
const handleClick = jest.fn();
render(<Button onClick={handleClick} />);
await userEvent.click(screen.getByRole('button'));
expect(handleClick).toHaveBeenCalled();
});
});
```
### Vue Component Tests
```javascript
describe('Button', () => {
it('renders without crashing', () => {
const wrapper = mount(Button);
expect(wrapper.exists()).toBe(true);
});
it('emits click event', async () => {
const wrapper = mount(Button);
await wrapper.trigger('click');
expect(wrapper.emitted('click')).toBeTruthy();
});
});
```
### Function Tests
```javascript
describe('formatCurrency', () => {
it('formats numbers correctly', () => {
expect(formatCurrency(1234.56)).toBe('$1,234.56');
});
it('handles edge cases', () => {
expect(formatCurrency(null)).toBe('$0.00');
expect(formatCurrency(0)).toBe('$0.00');
});
});
```
## Configuration Options
### TestGenerator Options
```javascript
{
framework: 'jest' | 'vitest' | 'mocha', // Test framework
assertionStyle: 'expect' | 'assert', // Assertion style
includeMocks: true, // Include mock generation
testType: 'unit' | 'integration' | 'e2e', // Test type
coverage: true, // Include coverage
environment: 'jsdom' | 'node' // Test environment
}
```
## Best Practices
### Test Organization
- **Co-location**: Keep tests near source files
- **Naming**: Use `.test.js` or `.spec.js` suffix
- **Structure**: Group related tests in `describe` blocks
- **Documentation**: Add meaningful test descriptions
### Test Quality
- **AAA Pattern**: Arrange, Act, Assert
- **Single Responsibility**: One assertion per test
- **Descriptive Names**: What behavior is being tested
- **Edge Cases**: Test null, undefined, errors
## Integration Examples
### Create React App
```bash
# Already has Jest configured
node scripts/generate-test.js src/components/Button.jsx
```
### Vite + Vue
```bash
# Setup Vitest
node scripts/setup-test-config.js vitest '{"vue": true}'
# Generate tests
node scripts/generate-test.js src/components/Button.vue
```
### Existing Mocha Project
```bash
# Detect setup
node scripts/detect-test-framework.js .
# Add new tests
node scripts/generate-test.js src/utils/format.js '{"framework": "mocha"}'
```
## Resources
### scripts/
Executable Node.js scripts for test generation and configuration:
- `detect-test-framework.js` - Analyzes existing testing setup
- `generate-test.js` - Creates test files from source code
- `setup-test-config.js` - Generates testing configuration files
### references/
Comprehensive documentation and guides:
- [test-patterns.md](references/test-patterns.md) - Testing patterns and best practices
- [framework-selection.md](references/framework-selection.md) - Framework comparison and selection guide
### assets/
Template files for common testing scenarios:
- [react-component.template.test.jsx](assets/react-component.template.test.jsx) - React component test template
- [vue-component.template.test.js](assets/vue-component.template.test.js) - Vue component test template
Related in Code Review
gstack
IncludedFast headless browser for QA testing and site dogfooding. Navigate pages, interact with elements, verify state, diff before/after, take annotated screenshots, test responsive layouts, forms, uploads, dialogs, and capture bug evidence. Use when asked to open or test a site, verify a deployment, dogfood a user flow, or file a bug with screenshots. (gstack)
startup-due-diligence
IncludedLegal due diligence review for seed-stage and Series A startups (US, Delaware C-Corp focus). Supports both investor and founder perspectives. Capabilities include: (1) Interactive document review and issue spotting; (2) Document request list generation; (3) Cap table and SAFE/convertible note analysis; (4) Red flag identification with severity ratings; (5) Diligence report generation. TRIGGERS: due diligence, DD, startup investment, cap table review, Series A, seed round, investor diligence, legal review startup, SAFE analysis, convertible note, 409A, founder vesting.
interview-master
IncludedThis skill should be used when the user asks to "generate interview questions", "prepare for interview", "optimize resume", "conduct mock interview", "analyze git commits for resume", "generate resume from code", "review my resume", or mentions interview preparation, career assistance, or extracting project experience from git history. Provides comprehensive interview and career development guidance for both job seekers and interviewers.
fix-issue
IncludedFixes GitHub issues using parallel analysis agents for root cause investigation, code exploration, and regression detection. Reads issue context from gh CLI, searches codebase and memory for related patterns, generates a fix with tests, and links the resolution back to the issue via PR. Includes prevention analysis to avoid recurrence. Use when debugging errors, resolving regressions, fixing bugs, or triaging issues.
sf-apex
IncludedGenerates and reviews Salesforce Apex code with 150-point scoring. TRIGGER when: user writes, reviews, or fixes Apex classes, triggers, test classes, batch/queueable/schedulable jobs, or touches .cls/.trigger files. DO NOT TRIGGER when: LWC JavaScript (use sf-lwc), Flow XML (use sf-flow), SOQL-only queries (use sf-soql), or non-Salesforce code.
swift-development
IncludedComprehensive Swift development for building, testing, and deploying iOS/macOS applications. Use when Claude needs to: (1) Build Swift packages or Xcode projects from command line, (2) Run tests with XCTest or Swift Testing framework, (3) Manage iOS simulators with simctl, (4) Handle code signing, provisioning profiles, and app distribution, (5) Format or lint Swift code with SwiftFormat/SwiftLint, (6) Work with Swift Package Manager (SPM), (7) Implement Swift 6 concurrency patterns (async/await, actors, Sendable), (8) Create SwiftUI views with MVVM architecture, (9) Set up Core Data or SwiftData persistence, or any other Swift/iOS/macOS development tasks.