測試過濾器
套件與測試的過濾、逾時及並行
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);
鉤子函數也能接收逾時時間,預設值同樣是 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');
});