Skip to content
Vitest 3
Main Navigation 指南 & API配置瀏覽器模式進階 API
3.2.0
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

API

Node API

進階 API

Vitest API

TestProject

TestSpecification

Test Task API

TestCase

TestSuite

TestModule

TestCollection

插件 API

執行器 API

報告器 API

任務中繼資料

指南

執行測試

擴展報告器

自訂 Pool

配置參考

測試 API 參考

本頁導覽

TestSpecification ​

TestSpecification 類別描述了作為測試執行之模組及其相關參數。

您只能透過呼叫測試專案的 createSpecification 方法來建立測試規格物件:

ts
const specification = project.createSpecification(
  resolve('./example.test.ts'),
  [20, 40] // optional test lines
);

createSpecification 預期接收已解析的模組 ID。它不會自動解析檔案路徑或檢查檔案系統上是否存在該檔案。

taskId ​

此為測試模組的唯一識別碼。

project ​

此屬性參考了測試模組所屬的 TestProject 實例。

moduleId ​

此為模組在 Vite 模組圖中的 ID。通常,它是使用 POSIX 分隔符號的絕對檔案路徑:

ts
'C:/Users/Documents/project/example.test.ts'; // ✅
'/Users/mac/project/example.test.ts'; // ✅
'C:\\Users\\Documents\\project\\example.test.ts'; // ❌

testModule ​

此為與規格相關聯的 TestModule 實例。如果測試尚未排入佇列,則此值將為 undefined。

pool 實驗性 ​

此為測試模組將執行於其中的 pool。

DANGER

在單一測試專案中,可以使用 poolMatchGlob 和 typecheck.enabled 來配置多個池。這意味著可能有多個具有相同 moduleId 但不同 pool 的測試規格物件。在 Vitest 4 中,專案將僅支援單一池,且此屬性將被移除。

testLines ​

此為原始碼中定義測試的行號陣列。此欄位僅在 createSpecification 方法接收到陣列時才被定義。

請注意,如果至少有一行未包含測試,則整個測試套件將會失敗。以下是 testLines 正確配置的範例:

ts
const specification = project.createSpecification(
  resolve('./example.test.ts'),
  [3, 8, 9]
);
ts
import { test, describe } from 'vitest'

test('verification works')

describe('a group of tests', () => { 
  // ...

  test('nested test')
  test.skip('skipped test')
})
1
2
3
4
5
6
7
8
9
10

toJSON ​

ts
function toJSON(): SerializedTestSpecification;

toJSON 會產生一個適合 JSON 格式的物件,可供 瀏覽器模式 或 Vitest UI 使用。

Pager
上一頁TestProject
下一頁TestCase

以 MIT 授權條款 發布。

版權所有 (c) 2021-Present Vitest Team

https://vitest.dev/advanced/api/test-specification

以 MIT 授權條款 發布。

版權所有 (c) 2021-Present Vitest Team