Vitest

npm install @tracera/vitest

已在 Node.js 22.19–26.12 和 Vitest 2–5.0 上测试(见 自动化测试适配器)。@tracera/vitest。

在 vitest.config.ts 中:

import { defineConfig } from 'vitest/config';
import { tracera } from '@tracera/vitest/plugin';

export default defineConfig({
  plugins: [tracera()],
});

插件会接入自定义 runner、setup 与 reporter。如需手动配置,仍可设置 test.runner / setupFiles / reporters。

环境变量、运行模式与重试见 自动化测试适配器。

重试时 Env 键:VITEST_RETRY。

绑定 testCaseId 与 autotestName

从 vitest 导入 test、expect 和 hooks,从 @tracera/vitest 导入 tracera 和 annotations。插件会接线 runner、setup 和 reporter,使标准 beforeEach / afterEach 映射到结果的 准备 / 清理。

在声明时(推荐 — 即使测试提前跳过也会绑定):

import { test, expect } from 'vitest';
import { annotations } from '@tracera/vitest';

test('login', { meta: annotations(42, 'e2e/login') }, async () => {
  expect(true).toBe(true);
});

在测试中(若 annotations 已绑定用例则可省略):

import { test, expect } from 'vitest';
import { tracera } from '@tracera/vitest';

test('login', async () => {
  tracera.testCase(42, 'e2e/login');
  expect(true).toBe(true);
});

Skip

使用 Vitest 标准 skip(test.skip,或测试上下文中的 skip(reason))Skip reason 作为 comment 添加到结果

import { test } from 'vitest';
import { annotations } from '@tracera/vitest';

test('login', { meta: annotations(42, 'e2e/login') }, async ({ skip }) => {
  if (!process.env.READY) skip('env not ready');
});

步骤

使用 tracera.step:调用 tracera.step(title, fn),在类方法上使用装饰器 @tracera.step("…"),或 tracera.step("…")(fn)。装饰器标题中的 {name} / {0} 从方法参数填入;也可以传入返回标题的函数。调用形式不会填充 {name} / {0}——请传入已拼好的标题字符串。会等待异步方法与 Promise。 使用 Tracera 插件(或显式 runner: "@tracera/vitest/runner")后,标准 beforeEach / afterEach 中的步骤进入结果的 准备 / 清理。套件级钩子(如 beforeAll / afterAll)不会上报。

import { test, beforeEach, afterEach } from 'vitest';
import { tracera } from '@tracera/vitest';

beforeEach(async () => {
  await tracera.step('open shop', async () => {
    // …
  });
});

test('checkout', async () => {
  tracera.testCase(10, 'e2e/checkout');
  await tracera.step('add to cart', async () => {
    // …
  });
});

afterEach(async () => {
  await tracera.step('cleanup', async () => {
    // …
  });
});

方法上的装饰器:

import { tracera } from '@tracera/vitest';

class Cart {
  @tracera.step('add {item}')
  async addToCart(item: string) {
    // …
  }
}

注释、错误与附件

评论、错误与附件:自动化测试适配器。

import { test } from 'vitest';
import { tracera } from '@tracera/vitest';

test('login', async () => {
  tracera.testCase(42, 'e2e/login');
  tracera.comment('result-level note');
  tracera.error('result-level error text');

  await tracera.step('open login', async () => {
    tracera.comment('landed on login');
    tracera.attachment({
      fileName: 'suite-log.txt',
      mimeType: 'text/plain',
      data: new TextEncoder().encode('finished login flow'),
    });
  });
});

参数化测试

使用 test.each / test.for 时,用 annotations(id, 'login as {login}', fn) 包裹回调——对象行的字段会填入 {login} 等占位符。可选 {param_id} 是该行的 Vitest 测试名。

import { test, expect } from 'vitest';
import { annotations } from '@tracera/vitest';

test.each([{ login: 'ok' }, { login: 'sso' }])(
  'login as $login',
  annotations(42, 'login as {login}', async ({ login }) => {
    expect(login).toBeTruthy();
  }),
);