logo
  • Guide
  • Config
  • Plugin
  • API
  • Examples
  • Community
  • Modern.js 2.x Docs
  • English
    • 简体中文
    • English
    • Start
      Introduction
      Quick Start
      Upgrading
      Glossary
      Tech Stack
      Core Concept
      Page Entry
      Build Engine
      Web Server
      Basic Features
      Routes
      Routing
      Config Routes
      Data Solution
      Data Fetching
      Data Writing
      Data Caching
      Rendering
      Server-Side Rendering
      Streaming SSR
      Rendering Cache
      Static Site Generation
      Render Preprocessing
      Styling
      Styling
      Use CSS Modules
      Using CSS-in-JS
      Using Tailwind CSS
      HTML Template
      Import Static Assets
      Import JSON Files
      Import SVG Assets
      Import Wasm Assets
      Debug
      Data Mocking
      Network Proxy
      Using Rsdoctor
      Using Storybook
      Testing
      Playwright
      Vitest
      Jest
      Cypress
      Path Alias
      Environment Variables
      Output Files
      Deploy Application
      Advanced Features
      Using Rspack
      Using BFF
      Basic Usage
      Runtime Framework
      Extend BFF Server
      Extend Request SDK
      File Upload
      Cross-Project Invocation
      Optimize Page Performance
      Code Splitting
      Inline Static Assets
      Bundle Size Optimization
      React Compiler
      Improve Build Performance
      Browser Compatibility
      Low-Level Tools
      Source Code Build Mode
      Server Monitor
      Monitors
      Logs Events
      Metrics Events
      Internationalization
      Basic Concepts
      Quick Start
      Configuration
      Locale Detection
      Resource Loading
      Routing Integration
      API Reference
      Advanced Usage
      Best Practices
      Custom Web Server
      Topic Detail
      Module Federation
      Introduction
      Getting Started
      Application-Level Modules
      Server-Side Rendering
      Deployment
      Integrating Internationalization
      FAQ
      Dependencies FAQ
      CLI FAQ
      Build FAQ
      HMR FAQ
      Deprecated
      📝 Edit this page
      Previous pagePlaywrightNext pageJest

      #Vitest

      Vitest is a testing framework driven by Vite, which can be used for unit testing in combination with React Testing Library.

      To use Vitest in Modern.js, you need to install the dependencies first. You can run the following commands:

      npm
      yarn
      pnpm
      bun
      npm install -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom
      yarn add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom
      pnpm install -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom
      bun add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom

      Next, you need to create a Vitest configuration file vitest.config.ts with the following content:

      vitest.config.ts
      import { defineConfig } from 'vitest/config'
      import react from '@vitejs/plugin-react'
       
      export default defineConfig({
        plugins: [react()],
        test: {
          environment: 'jsdom',
        },
      })

      For more information about Vitest configuration, you can refer to the Vitest configuration documentation.

      You can optionally add the vitest command to package.json:

      package.json
      {
        "scripts": {
          "test": "vitest"
        }
      }

      After running this command, Vitest will automatically watch your file changes and rerun the test cases.

      #Create Unit Tests

      First, create a simple page for testing:

      routes/page.tsx
      import { Link } from '@modern-js/runtime/router';
      
      const Index = () => (
        <div>
          <h1>Home</h1>
          <Link to="/about">About</Link>
        </div>
      );
      
      export default Index;

      Add a test case to check if the page has the expected text:

      __tests__/page.test.tsx
      import { expect, test } from 'vitest';
      import { render, screen } from '@testing-library/react';
      import { BrowserRouter as Router } from '@modern-js/runtime/router';
      import Page from '../routes/page';
      
      test('Page', () => {
        render(
          <Router>
            <Page />
          </Router>,
        );
        expect(screen.getByRole('heading', { level: 1, name: 'Home' })).toBeDefined();
      });

      In the above test case, we imported the <Router> component from @modern-js/runtime/router because React Router requires the corresponding context when rendering some route-related components.

      Note

      When running directly in the Modern.js application, the <Router> component will be automatically injected.

      #Run Test Cases

      Execute the above test command to run the test cases:

      ✓ src/__tests__/page.test.tsx (1)
        ✓ Page
      
      Test Files  1 passed (1)
          Tests  1 passed (1)
        Start at  15:37:12
        Duration  999ms (transform 119ms, setup 0ms, collect 365ms, tests 33ms, environment 421ms, prepare 44ms)
      
      
      PASS  Waiting for file changes...