Skip to content
Vitest 2
Main Navigation 指南API配置瀏覽器模式高級
2.1.9
1.6.1
0.34.6

繁體中文

English
简体中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

繁體中文

English
简体中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

外觀

Sidebar Navigation

為什麼使用 Vitest

開始使用

功能特性

工作區

命令列界面

測試過濾器

報告器

覆蓋率

快照

模擬(Mocking)

測試類型

Vitest UI

原始碼測試

測試上下文

測試環境

擴展匹配器

IDE 整合支援

偵錯

與其他測試執行器的比較

遷移指南

常見錯誤

Profiling Test Performance

提升效能

本頁導覽

提升效能 ​

測試隔離 ​

預設情況下,Vitest 會根據 pool 在隔離的環境中執行每個測試檔案:

  • threads pool 會在單獨的 Worker 中執行每個測試檔案。
  • forks pool 會在單獨的 forked child process 中執行每個測試檔案。
  • vmThreads pool 會在單獨的 VM context 中執行每個測試檔案,但它使用 worker 進行平行處理。

這將大幅增加測試所需時間。對於不依賴副作用且能正確清理其狀態的專案(通常是 node 環境的專案)來說,這可能不是理想的選擇。在這種情況下,停用隔離將提高測試速度。為此,您可以向 CLI 提供 --no-isolate 參數,或將設定檔中的 test.isolate 屬性設定為 false。如果您同時使用多個 pool 和 poolMatchGlobs,您也可以為您正在使用的特定 pool 停用隔離。

bash
vitest --no-isolate
ts
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    isolate: false,
    // 您也可以只為特定的 pool 停用隔離
    poolOptions: {
      forks: {
        isolate: false,
      },
    },
  },
});

TIP

如果您使用 vmThreads pool,則無法停用隔離。請改用 threads pool 以提升測試效能。

對於某些專案,停用平行處理以提高啟動速度可能也是有益的。為此,請向 CLI 提供 --no-file-parallelism 參數,或將設定檔中的 test.fileParallelism 屬性設定為 false。

bash
vitest --no-file-parallelism
ts
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    fileParallelism: false,
  },
});

Pool ​

預設情況下,Vitest 在 pool: 'forks' 模式下執行測試。雖然 'forks' pool 對於相容性議題(程序掛起和 segfaults)表現較好,但在較大的專案中,它可能比 pool: 'threads' 稍慢。

您可以嘗試透過切換設定檔中的 pool 選項來縮短測試執行時間:

bash
vitest --pool=threads
ts
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    pool: 'threads',
  },
});

分片 ​

測試分片是指一次執行一小部分測試用例。當您有多台機器可以同時執行測試時,這會很有幫助。

若要將 Vitest 測試分割成多個不同的執行個體,請使用 --shard 選項搭配 --reporter=blob 選項:

sh
vitest run --reporter=blob --shard=1/3 # 1st machine
vitest run --reporter=blob --shard=2/3 # 2nd machine
vitest run --reporter=blob --shard=3/3 # 3rd machine

從每台機器取得儲存在 .vitest-reports 目錄中的結果,並使用 --merge-reports 選項將它們合併:

sh
vitest --merge-reports
Github action 範例

此設定也用於 https://github.com/vitest-tests/test-sharding。

yaml
# 參考自 https://playwright.dev/docs/test-sharding
name: Tests
on:
  push:
    branches:
      - main
jobs:
  tests:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        shardIndex: [1, 2, 3, 4]
        shardTotal: [4]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install pnpm
        uses: pnpm/action-setup@v4

      - name: Install dependencies
        run: pnpm i

      - name: Run tests
        run: pnpm run test --reporter=blob --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}

      - name: Upload blob report to GitHub Actions Artifacts
        if: ${{ !cancelled() }}
        uses: actions/upload-artifact@v4
        with:
          name: blob-report-${{ matrix.shardIndex }}
          path: .vitest-reports/*
          include-hidden-files: true
          retention-days: 1

  merge-reports:
    if: ${{ !cancelled() }}
    needs: [tests]

    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install pnpm
        uses: pnpm/action-setup@v4

      - name: Install dependencies
        run: pnpm i

      - name: Download blob reports from GitHub Actions Artifacts
        uses: actions/download-artifact@v4
        with:
          path: .vitest-reports
          pattern: blob-report-*
          merge-multiple: true

      - name: Merge reports
        run: npx vitest --merge-reports

TIP

在高 CPU 數量的機器上,測試分片也可能變得有益。

Vitest 只會在其主執行緒中執行一個 Vite 伺服器。其餘的執行緒則用於執行測試檔案。 在高 CPU 數量的機器中,主執行緒可能會成為瓶頸,因為它無法處理來自所有執行緒的所有請求。例如,在 32 CPU 的機器中,主執行緒負責處理來自 31 個測試執行緒的工作負載。

為了減少主執行緒的 Vite 伺服器工作負載,您可以使用測試分片。工作負載可以分散到多個 Vite 伺服器上。

sh
# 將 32 CPU 上的測試分割成 4 個分片的範例。
# 由於每個程序需要 1 個主執行緒,因此有 7 個執行緒用於測試執行器 (1+7)*4 = 32
# 根據 pool 使用 VITEST_MAX_THREADS 或 VITEST_MAX_FORKS:
VITEST_MAX_THREADS=7 vitest run --reporter=blob --shard=1/4 & \
VITEST_MAX_THREADS=7 vitest run --reporter=blob --shard=2/4 & \
VITEST_MAX_THREADS=7 vitest run --reporter=blob --shard=3/4 & \
VITEST_MAX_THREADS=7 vitest run --reporter=blob --shard=4/4 & \
wait # https://man7.org/linux/man-pages/man2/waitpid.2.html

vitest --merge-reports
Pager
上一頁Profiling Test Performance

以 MIT 授權條款 發布。

版權所有 (c) 2024 Mithril Contributors

https://v2.vitest.dev/guide/improving-performance

以 MIT 授權條款 發布。

版權所有 (c) 2024 Mithril Contributors