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')
})
toJSON
ts
function toJSON(): SerializedTestSpecification;