Skip to content
Vitest 2
Main Navigation 指南API配置浏览器模式高级
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

为什么选择 Vitest

快速开始

特性

工作区

命令行界面

测试筛选

报告器

代码覆盖率

快照(Snapshot)

模拟(Mocking)

类型测试

Vitest UI

源码内测试

测试上下文

测试环境

扩展匹配器

IDE 集成

调试

与其他测试运行器的比较

迁移指南

常见错误

Profiling Test Performance

提升性能

页面导航

测试筛选 ​

测试套件和测试用例的筛选、超时与并发

命令行 (CLI) ​

你可以使用命令行 (CLI) 按名称筛选测试文件:

bash
$ vitest basic

只会执行文件名中包含 basic 的测试文件,例如:

basic.test.ts
basic-foo.test.ts
basic/foo.test.ts

你还可以使用 -t, --testNamePattern <pattern> 选项通过完整名称来过滤测试。当你想通过文件中定义的名称而不是文件名本身进行过滤时,这会很有用。

指定超时时间 ​

你可以选择性地为测试传递一个毫秒级的超时时间作为第三个参数。默认值是 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');
});
Pager
上一页命令行界面
下一页报告器

基于 MIT 许可证 发布。

版权所有 (c) 2024 Mithril Contributors

https://v2.vitest.dev/guide/filtering

基于 MIT 许可证 发布。

版权所有 (c) 2024 Mithril Contributors