Skip to content
Vitest 1
Main Navigation 指南API配置高級
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 整合支援

偵錯

與其他測試執行器的比較

遷移指南

常見錯誤

提升效能

API

測試 API 參考文件

模擬函數

Vi

expect

expectTypeOf

assert

assertType

配置

管理 Vitest 配置文件

配置 Vitest

本頁導覽

測試上下文 ​

受到 Playwright Fixtures 的啟發,Vitest 的測試上下文允許您定義可在測試中使用的工具、狀態和測試夾具。

用法 ​

每個測試回調函數的第一個參數都是一個測試上下文。

ts
import { it } from 'vitest';

it('should work', ctx => {
  // 打印測試名稱
  console.log(ctx.task.name);
});

內建測試上下文 ​

context.task ​

一個唯讀的物件,包含關於測試的元數據。

context.expect ​

綁定到當前測試的 expect API:

ts
import { it } from 'vitest';

it('math is easy', ({ expect }) => {
  expect(2 + 2).toBe(4);
});

這個 API 對於並行執行快照測試非常有用,因為全域 expect 無法追蹤它們:

ts
import { it } from 'vitest';

it.concurrent('math is easy', ({ expect }) => {
  expect(2 + 2).toMatchInlineSnapshot();
});

it.concurrent('math is hard', ({ expect }) => {
  expect(2 * 2).toMatchInlineSnapshot();
});

context.skip ​

跳過後續的測試執行,並將測試標記為跳過:

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

it('math is hard', ({ skip }) => {
  skip();
  expect(2 + 2).toBe(5);
});

擴充測試上下文 ​

Vitest 提供了兩種方式來幫助您擴充測試上下文。

test.extend ​

WARNING

此 API 自 Vitest 0.32.3 起可用。

與 Playwright 類似,您可以使用此方法定義您自己的 test API,其中包含自訂的測試夾具,並在任何地方複用它。

例如,我們首先使用兩個測試夾具 todos 和 archive 建立 myTest。

ts
// my-test.ts
import { test } from 'vitest';

const todos = [];
const archive = [];

export const myTest = test.extend({
  todos: async ({ task }, use) => {
    // 在每個測試函數之前設定夾具
    todos.push(1, 2, 3);

    // 使用夾具數值
    await use(todos);

    // 在每個測試函數之後清理夾具
    todos.length = 0;
  },
  archive,
});

然後我們可以導入並使用它。

ts
import { expect } from 'vitest';
import { myTest } from './my-test.ts';

myTest('add items to todos', ({ todos }) => {
  expect(todos.length).toBe(3);

  todos.push(4);
  expect(todos.length).toBe(4);
});

myTest('move items from todos to archive', ({ todos, archive }) => {
  expect(todos.length).toBe(3);
  expect(archive.length).toBe(0);

  archive.push(todos.pop());
  expect(todos.length).toBe(2);
  expect(archive.length).toBe(1);
});

我們還可以透過擴充 myTest 來新增更多測試夾具或覆蓋現有的測試夾具。

ts
export const myTest2 = myTest.extend({
  settings: {
    // ...
  },
});

夾具初始化 ​

Vitest 執行器將根據使用情況,智能地初始化您的夾具並將它們注入至測試上下文中。

ts
import { test } from 'vitest';

async function todosFn({ task }, use) {
  await use([1, 2, 3]);
}

const myTest = test.extend({
  todos: todosFn,
  archive: [],
});

// todosFn 不會執行
myTest('', () => {});
myTest('', ({ archive }) => {});

// todosFn 會執行
myTest('', ({ todos }) => {});

WARNING

當使用帶有測試夾具的 test.extend() 時,您應該始終使用物件解構模式 { todos } 來訪問夾具函數和測試函數中的上下文。

自動 fixture ​

WARNING

此功能在 Vitest 1.3.0 版本中可用。

Vitest 也支援 fixture 的元組語法, 允許你為每個 fixture 傳遞選項。 例如,你可以使用它來顯式地初始化一個 fixture,即使它沒有在測試中使用。

ts
import { test as base } from 'vitest';

const test = base.extend({
  fixture: [
    async ({}, use) => {
      // 此函数将会运行
      setup();
      await use();
      teardown();
    },
    { auto: true }, // 標記為一個自動 fixture
  ],
});

test('', () => {});

TypeScript ​

要為所有自訂上下文提供夾具類型,您可以將夾具類型作為泛型傳入。

ts
interface MyFixtures {
  todos: number[];
  archive: number[];
}

const myTest = test.extend<MyFixtures>({
  todos: [],
  archive: [],
});

myTest('', context => {
  expectTypeOf(context.todos).toEqualTypeOf<number[]>();
  expectTypeOf(context.archive).toEqualTypeOf<number[]>();
});

beforeEach 和 afterEach ​

每個測試的上下文都不同。您可以在 beforeEach 和 afterEach 鉤子中存取和擴充它們。

ts
import { beforeEach, it } from 'vitest';

beforeEach(async context => {
  // 擴充上下文
  context.foo = 'bar';
});

it('should work', ({ foo }) => {
  console.log(foo); // 'bar'
});

TypeScript ​

要為所有自訂上下文提供屬性類型,您可以透過添加以下內容聚合 TestContext 類型

ts
declare module 'vitest' {
  export interface TestContext {
    foo?: string;
  }
}

如果您只想為特定的 beforeEach、afterEach、it 和 test 鉤子提供屬性類型,您可以將類型作為泛型傳遞。

ts
interface LocalTestContext {
  foo: string;
}

beforeEach<LocalTestContext>(async context => {
  // context 的類型是 'TestContext & LocalTestContext'
  context.foo = 'bar';
});

it<LocalTestContext>('should work', ({ foo }) => {
  // foo 的類型是 'string'
  console.log(foo); // 'bar'
});
Pager
上一頁原始碼測試
下一頁測試環境

以 MIT 授權條款 發布。

版權所有 (c) 2024 Mithril Contributors

https://v1.vitest.dev/guide/test-context

以 MIT 授權條款 發布。

版權所有 (c) 2024 Mithril Contributors