Back to Skills

Vitest

oriolrius
Updated 7 days ago
14 views
1
1
1
View on GitHub
Testingreacttestingdesign

About

This skill provides expert guidance for the Vitest testing framework, helping developers write tests for Vite-based applications. It covers unit tests, integration tests, mocking, coverage, and integration with React Testing Library and TypeScript. Use it when you need assistance with Vitest's Jest-compatible API, fast execution, and built-in testing features.

Documentation

Vitest

Expert assistance with Vitest - Blazing fast unit test framework.

Overview

Vitest is a Vite-native testing framework:

  • Fast: Powered by Vite, instant HMR
  • Compatible: Jest-compatible API
  • TypeScript: First-class TypeScript support
  • Coverage: Built-in coverage with v8/istanbul
  • UI: Beautiful UI for test results

Installation

npm install --save-dev vitest
npm install --save-dev @vitest/ui
npm install --save-dev @testing-library/react @testing-library/jest-dom

Configuration

// vitest.config.ts
import { defineConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,
    environment: 'jsdom',
    setupFiles: './src/test/setup.ts',
    coverage: {
      provider: 'v8',
      reporter: ['text', 'html', 'json'],
    },
  },
});

Setup File

// src/test/setup.ts
import { expect, afterEach } from 'vitest';
import { cleanup } from '@testing-library/react';
import * as matchers from '@testing-library/jest-dom/matchers';

expect.extend(matchers);

afterEach(() => {
  cleanup();
});

Basic Tests

import { describe, it, expect } from 'vitest';

describe('Math functions', () => {
  it('adds numbers', () => {
    expect(1 + 1).toBe(2);
  });

  it('multiplies numbers', () => {
    const result = 2 * 3;
    expect(result).toEqual(6);
  });
});

Testing React Components

import { render, screen } from '@testing-library/react';
import { describe, it, expect } from 'vitest';
import { Button } from './Button';

describe('Button', () => {
  it('renders with text', () => {
    render(<Button>Click me</Button>);
    expect(screen.getByText('Click me')).toBeInTheDocument();
  });

  it('handles click events', async () => {
    const handleClick = vi.fn();
    render(<Button onClick={handleClick}>Click</Button>);

    await userEvent.click(screen.getByText('Click'));
    expect(handleClick).toHaveBeenCalledOnce();
  });
});

Mocking

import { vi } from 'vitest';

// Mock function
const mockFn = vi.fn();
mockFn('hello');
expect(mockFn).toHaveBeenCalledWith('hello');

// Mock return value
const mockFn = vi.fn().mockReturnValue(42);
expect(mockFn()).toBe(42);

// Mock async function
const mockFn = vi.fn().mockResolvedValue('data');
const result = await mockFn();
expect(result).toBe('data');

// Mock module
vi.mock('./api', () => ({
  fetchCertificate: vi.fn().mockResolvedValue({ id: '1', subject: 'CN=test' }),
}));

Best Practices

  1. Describe Blocks: Group related tests
  2. Clear Names: Write descriptive test names
  3. AAA Pattern: Arrange, Act, Assert
  4. One Assertion: Test one thing at a time
  5. Mock External: Mock external dependencies
  6. Coverage: Aim for high coverage
  7. Fast Tests: Keep tests fast
  8. Isolation: Tests should be independent
  9. User Events: Use userEvent over fireEvent
  10. Accessibility: Test with accessible queries

Resources

Quick Install

/plugin add https://github.com/oriolrius/pki-manager-web/tree/main/vitest

Copy and paste this command in Claude Code to install this skill

GitHub 仓库

oriolrius/pki-manager-web
Path: .claude/skills/vitest
certificate-authoritycertificate-managementcosmianfastifykmspki

Related Skills

evaluating-llms-harness

Testing

This Claude Skill runs the lm-evaluation-harness to benchmark LLMs across 60+ standardized academic tasks like MMLU and GSM8K. It's designed for developers to compare model quality, track training progress, or report academic results. The tool supports various backends including HuggingFace and vLLM models.

View skill

langchain

Meta

LangChain is a framework for building LLM applications using agents, chains, and RAG pipelines. It supports multiple LLM providers, offers 500+ integrations, and includes features like tool calling and memory management. Use it for rapid prototyping and deploying production systems like chatbots, autonomous agents, and question-answering services.

View skill

Algorithmic Art Generation

Meta

This skill helps developers create algorithmic art using p5.js, focusing on generative art, computational aesthetics, and interactive visualizations. It automatically activates for topics like "generative art" or "p5.js visualization" and guides you through creating unique algorithms with features like seeded randomness, flow fields, and particle systems. Use it when you need to build reproducible, code-driven artistic patterns.

View skill

webapp-testing

Testing

This Claude Skill provides a Playwright-based toolkit for testing local web applications through Python scripts. It enables frontend verification, UI debugging, screenshot capture, and log viewing while managing server lifecycles. Use it for browser automation tasks but run scripts directly rather than reading their source code to avoid context pollution.

View skill