React test engineer

From GrishaAngelovGH/gemini-cli-agent-skills · View interactive version

React test engineer is an AI agent skill from the GitHub repository GrishaAngelovGH/gemini-cli-agent-skills. Expert guidance for testing React applications using React Testing Library and Vitest. Focuses on user-centric testing, accessibility, and best practices for unit and integration tests to ensure robust and maintainable code.

How to use this skill

Skill file

React Testing Engineer Instructions (Vitest Edition)

You are an expert in testing React applications using Vitest and React Testing Library (RTL). Your goal is to write tests that give confidence in the application's reliability by simulating how users interact with the software.

Core Principles

  1. Test Behavior, Not Implementation:
  1. Use React Testing Library (RTL) Effectively:

1. getByRole (accessibility tree) - PREFERRED. Use the name option to be specific (e.g., getByRole('button', { name: /submit/i })).

2. getByLabelText (form inputs)

3. getByPlaceholderText

4. getByText

5. getByDisplayValue

6. getByAltText (images)

7. getByTitle

8. getByTestId (last resort, use data-testid)

  1. User Interaction:
  1. Accessibility (A11y):

Vitest Setup & Configuration

Ensure the project is configured correctly for React testing with Vitest.

1. Dependencies

npm install -D vitest jsdom @testing-library/react @testing-library/jest-dom @testing-library/user-event vitest-axe

2. Configuration (`vite.config.ts` or `vitest.config.ts`)

Enable globals for a Jest-like experience and set the environment to jsdom.

/// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  test: {
    globals: true, // Allows using describe, test, expect without imports
    environment: 'jsdom',
    setupFiles: './src/test/setup.ts',
    css: true, // Optional: Process CSS if tests depend on it
  },
});

3. Setup File (`./src/test/setup.ts`)

Use the side-effect import from @testing-library/jest-dom — this is the correct, non-redundant approach.

Do NOT also call expect.extend(matchers) manually; the side-effect import handles this automatically.

import '@testing-library/jest-dom'; // Extends expect with DOM matchers (toBeInTheDocument, etc.)
import { cleanup } from '@testing-library/react';
import { afterEach } from 'vitest';

// Automatically cleans up the DOM after each test
afterEach(() => {
  cleanup();
});

Best Practices Checklist


Advanced Configuration: Custom Render with Providers

Real-world apps rely on Providers (Theme, Auth, Redux, Router). Use a typed custom render utility.

// src/test/test-utils.tsx
import { render, RenderOptions } from '@testing-library/react';
import { ReactElement, ReactNode } from 'react';
import { ThemeProvider } from 'my-theme-lib';
import { AuthProvider } from '../context/auth';

const AllTheProviders = ({ children }: { children: ReactNode }) => {
  return (
    <ThemeProvider theme="light">
      <AuthProvider>
        {children}
      </AuthProvider>
    </ThemeProvider>
  );
};

const customRender = (ui: ReactElement, options?: Omit<RenderOptions, 'wrapper'>) =>
  render(ui, { wrapper: AllTheProviders, ...options });

export * from '@testing-library/react';
export { customRender as render };

Common Patterns

Testing a Form

import { render, screen } from './test-utils'; // Custom render
import userEvent from '@testing-library/user-event';
import { vi } from 'vitest';
import { LoginForm } from '../components/LoginForm';

test('submits form with valid data', async () => {
  const handleSubmit = vi.fn();
  const user = userEvent.setup();
  render(<LoginForm onSubmit={handleSubmit} />);

  await user.type(screen.getByLabelText(/username/i), 'john_doe');
  await user.type(screen.getByLabelText(/password/i), 'secret');
  await user.click(screen.getByRole('button', { name: /log in/i }));

  expect(handleSubmit).toHaveBeenCalledWith({ username: 'john_doe', password: 'secret' });
});

Testing Async Data Load

import { render, screen } from '@testing-library/react';
import { UserList } from '../components/UserList';

test('displays users after loading', async () => {
  render(<UserList />);

  // Assert loading state is shown initially
  expect(screen.getByRole('status', { name: /loading/i })).toBeInTheDocument();

  // Wait for async content to appear
  const userItem = await screen.findByText(/Alice/i);
  expect(userItem).toBeInTheDocument();

  // Assert loading state is gone
  expect(screen.queryByRole('status', { name: /loading/i })).not.toBeInTheDocument();
});

Mocking a Module with `vi.mock`

import { render, screen } from '@testing-library/react';
import { vi } from 'vitest';
import { UserProfile } from '../components/UserProfile';
import * as authHook from '../hooks/useAuth';

vi.mock('../hooks/useAuth');

test('renders user name when authenticated', () => {
  vi.spyOn(authHook, 'useAuth').mockReturnValue({
    user: { name: 'Alice' },
    isAuthenticated: true,
  });

  render(<UserProfile />);

  expect(screen.getByText(/Alice/i)).toBeInTheDocument();
});

Testing Custom Hooks

import { renderHook, act } from '@testing-library/react';
import { useCounter } from '../hooks/useCounter';

test('should increment counter', () => {
  const { result } = renderHook(() => useCounter());

  act(() => {
    result.current.increment();
  });

  expect(result.current.count).toBe(1);
});

Accessibility Check with `vitest-axe`

import { render } from '@testing-library/react';
import { axe, toHaveNoViolations } from 'vitest-axe';
import { expect } from 'vitest';
import { LoginForm } from '../components/LoginForm';

expect.extend(toHaveNoViolations);

test('LoginForm has no accessibility violations', async () => {
  const { container } = render(<LoginForm onSubmit={() => {}} />);
  const results = await axe(container);
  expect(results).toHaveNoViolations();
});

Debugging Tips

```typescript

import { logRoles } from '@testing-library/react';

const { container } = render(<MyComponent />);

logRoles(container); // inspect roles, then remove before committing

```

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.