Skip to content
Back to skills

979 Skill C8810f4f

ASecurity

Add unit tests for a component or function

  • 11 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 10, 2026
testingtypescriptbashreacttesting

Works with

  • cli

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned November 10, 2026

npx -y skills add tools-only/X-Skills --skill 979-skill_c8810f4f --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 979 Skill C8810f4f?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 979 Skill C8810f4f
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-979-skill-c8810f4f/badge)](https://www.skillsdirectory.com/skills/tools-only-979-skill-c8810f4f)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
skill: add-test
description: Add unit tests for a component or function
arguments: component or function name
---

# Add Tests: $ARGUMENTS

Create comprehensive unit tests for the specified component or function.

## Process

### 1. Locate Source File

Search in order:
- `components/$ARGUMENTS.tsx` (React component)
- `lib/$ARGUMENTS.ts` (utility function)
- `app/$ARGUMENTS.tsx` (page component)

### 2. Analyze Source

Read the file to identify:
- Exported functions/components
- Props interfaces
- State management
- Event handlers
- Key functionality to test

### 3. Create Test File

Place test adjacent to source: `[name].test.tsx` or `[name].test.ts`

### 4. Generate Tests

**For React components:**

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

describe('ComponentName', () => {
  test('renders correctly', () => {
    render(<ComponentName />);
    expect(screen.getByTestId('component-name')).toBeInTheDocument();
  });

  test('handles user interaction', () => {
    render(<ComponentName />);
    fireEvent.click(screen.getByRole('button'));
    // assert expected behavior
  });

  test('handles empty state', () => { /* ... */ });
  test('handles error state', () => { /* ... */ });
});
```

**For utility functions:**

```typescript
import { functionName } from './fileName';

describe('functionName', () => {
  test('returns expected output', () => {
    expect(functionName(input)).toBe(expected);
  });

  test('handles empty input', () => { /* ... */ });
  test('handles edge cases', () => { /* ... */ });
  test('throws on invalid input', () => {
    expect(() => functionName(invalid)).toThrow();
  });
});
```

### 5. Run Tests

```bash
npm test -- $ARGUMENTS.test
```

Fix any failures, then report results.

## Coverage Requirements

- Happy path scenarios
- Edge cases (empty, null, boundary values)
- Error states
- User interactions (for components)
- Accessibility (keyboard navigation)

## Best Practices

- Use `data-testid` for stable selectors
- Mock localStorage if needed
- Tests should be independent and deterministic
- Descriptive test names that explain the scenario

Files in this skill

  • README.md853 B
  • skill.md2.1 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in testing →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.