vitest-skill

Generates Vitest tests in JavaScript/TypeScript with Vite-native speed. Jest-compatible API with ESM support and HMR. Use when user mentions "Vitest", "vi.mock", "vitest.config". Triggers on: "Vitest", "vi.mock", "vi.fn", "Vite test", "vitest config".

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "vitest-skill" with this command: npx skills add lambdatest/agent-skills/lambdatest-agent-skills-vitest-skill

Vitest Testing Skill

Core Patterns

Basic Test

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { Calculator } from './calculator';

describe('Calculator', () => {
  let calc: Calculator;
  beforeEach(() => { calc = new Calculator(); });

  it('adds two numbers', () => {
    expect(calc.add(2, 3)).toBe(5);
  });

  it('throws on divide by zero', () => {
    expect(() => calc.divide(10, 0)).toThrow();
  });
});

Mocking (vi instead of jest)

import { vi } from 'vitest';

// Mock module
vi.mock('./database', () => ({
  getUser: vi.fn().mockResolvedValue({ name: 'Alice' }),
  saveUser: vi.fn().mockResolvedValue(true),
}));

// Mock function
const mockFn = vi.fn();
mockFn.mockReturnValue(42);
mockFn.mockResolvedValue({ data: 'test' });

// Spy
const spy = vi.spyOn(console, 'log').mockImplementation(() => {});
expect(spy).toHaveBeenCalledWith('message');
spy.mockRestore();

// Timers
vi.useFakeTimers();
vi.advanceTimersByTime(1000);
vi.runAllTimers();
vi.useRealTimers();

In-Source Testing

// src/math.ts — tests alongside code!
export function add(a: number, b: number) { return a + b; }
export function multiply(a: number, b: number) { return a * b; }

if (import.meta.vitest) {
  const { it, expect } = import.meta.vitest;
  it('adds', () => { expect(add(2, 3)).toBe(5); });
  it('multiplies', () => { expect(multiply(3, 4)).toBe(12); });
}

Snapshot Testing

it('serializes user', () => {
  expect(serializeUser(user)).toMatchSnapshot();
});

it('inline snapshot', () => {
  expect(serializeUser(user)).toMatchInlineSnapshot(`
    { "name": "Alice", "email": "alice@test.com" }
  `);
});

React Component Testing

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

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

Anti-Patterns

BadGoodWhy
jest.fn()vi.fn()Vitest uses vi
jest.mock()vi.mock()Different namespace
No type safetyTypeScript + strictVitest is TS-first

Quick Reference

TaskCommand
Run oncenpx vitest run
Watchnpx vitest (default)
UInpx vitest --ui
Coveragenpx vitest --coverage
Specific filenpx vitest run src/math.test.ts
Filternpx vitest run -t "adds"

vitest.config.ts

import { defineConfig } from 'vitest/config';
export default defineConfig({
  test: {
    globals: true,
    environment: 'jsdom',
    coverage: { provider: 'v8', reporter: ['text', 'html'] },
    include: ['src/**/*.{test,spec}.{js,ts,tsx}'],
    includeSource: ['src/**/*.{js,ts}'],
  },
});

Deep Patterns → reference/playbook.md

§SectionLines
1Production ConfigurationConfig, workspace, setup file
2Mocking Patternsvi.mock, spies, timers, fetch
3React Testing LibraryComponents, hooks, providers
4Snapshot & Inline SnapshotsFile, inline, serializers
5Table-Driven Teststest.each, describe.each
6In-Source TestingCo-located tests in source
7API / Integration TestingServer tests with fetch
8CI/CD IntegrationGitHub Actions, scripts
9Debugging Quick-Reference10 common problems
10Best Practices Checklist13 items

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Automation

appium-skill

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

selenium-skill

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

playwright-skill

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

jasmine-skill

No summary provided by upstream source.

Repository SourceNeeds Review