Skill Instructions
When the user asks to test a React component:
- •Library Choice: Always use
@testing-library/reactandvitest. - •Patterns: Prefer
user-eventoverfireEventfor user interactions. - •Mocks: Automatically mock global fetch or external API hooks using
vi.mock. - •Structure:
- •Wrap tests in a
describeblock named after the component. - •Use
it(nottest) for individual test cases. - •Include a test case for "renders without crashing".
- •Wrap tests in a
- •Accessibility: Prioritize
getByRoleandfindByRoleovergetByTestId.
Example Template
javascript
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, it, expect, vi } from 'vitest';
import MyComponent from './MyComponent';
describe('MyComponent', () => {
it('renders correctly', () => {
render(<MyComponent />);
expect(screen.getByRole('heading')).toBeInTheDocument();
});
});