测试筛选
测试套件和测试用例的筛选、超时与并发
命令行 (CLI)
你可以使用命令行 (CLI) 按名称筛选测试文件:
bash
$ vitest basic
只会执行文件名中包含 basic
的测试文件,例如:
basic.test.ts
basic-foo.test.ts
basic/foo.test.ts
指定超时时间
你可以选择将超时时间(以毫秒为单位)作为测试的第三个参数传递。默认值为 5 秒。
ts
import { test } from 'vitest';
test('name', async () => {
/* ... */
}, 1000);
钩子函数 (Hooks) 也可以接收超时时间,默认值同样为 5 秒。
ts
import { beforeAll } from 'vitest';
beforeAll(async () => {
/* ... */
}, 1000);
跳过套件和测试
使用 .skip
跳过某些套件或测试
ts
import { assert, describe, it } from 'vitest';
describe.skip('skipped suite', () => {
it('test', () => {
// 套件已跳过,无错误
assert.equal(Math.sqrt(4), 3);
});
});
describe('suite', () => {
it.skip('skipped test', () => {
// 测试已跳过,无错误
assert.equal(Math.sqrt(4), 3);
});
});
选择要执行的套件和测试
使用 .only
仅运行某些套件或测试
ts
import { assert, describe, it } from 'vitest';
// 仅运行此套件(以及其他标记为 only 的套件)
describe.only('suite', () => {
it('test', () => {
assert.equal(Math.sqrt(4), 3);
});
});
describe('another suite', () => {
it('skipped test', () => {
// 测试被跳过,因为当前处于 Only 模式
assert.equal(Math.sqrt(4), 3);
});
it.only('test', () => {
// 仅运行此测试(以及其他标记为 only 的测试)
assert.equal(Math.sqrt(4), 2);
});
});
未实现的套件和测试
使用 .todo
来占位 (stub) 待实现的套件和测试
ts
import { describe, it } from 'vitest';
// 测试报告中会显示该套件的记录项
describe.todo('unimplemented suite');
// 报告中将显示此测试的条目
describe('suite', () => {
it.todo('unimplemented test');
});