TestSpecification
TestSpecification
类描述了作为测试运行的模块及其相关参数。
你只能通过调用测试项目上的 createSpecification
方法来创建测试规范:
ts
const specification = project.createSpecification(
resolve('./example.test.ts'),
[20, 40] // 可选的测试行
);
createSpecification
方法需要已解析的模块 ID。它不会自动解析文件路径,也不会检查文件系统上是否存在该文件。
taskId
测试模块的唯一标识符。
project
此属性指向测试模块所属的 TestProject
实例。
moduleId
模块在 Vite 模块图中的标识符。通常,它是一个使用 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
中指定的行号至少有一行没有对应的测试,则整个测试套件将失败。以下是一个正确的 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;