Jest

npm install @tracera/jest

需要 Node.js ≥ 22.19(见 自动化测试适配器)。@tracera/jest。

在 jest.config.cjs(或你的 Jest 配置)中:

module.exports = {
  testEnvironment: 'node',
  setupFilesAfterEnv: ['@tracera/jest/setup'],
  reporters: ['default', '@tracera/jest/reporter'],
};

启用重试时只上报 最终 一次 Jest 尝试。若该次是重试(retry > 0),结果 Env 页会包含 JEST_RETRY(1 表示第二次运行)。

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

绑定 testCaseId 与 autotestName

从 @jest/globals 导入 test / expect / hooks(或使用 Jest 注入的全局变量),从 @tracera/jest 导入 tracera / annotations / skip。@tracera/jest/setup 会仪器化 runner,使步骤与绑定可配合标准 Jest API 使用。若未设置 autotestName,适配器使用 Jest 测试标题。

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

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

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

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

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

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

跳过

若事先已知应跳过,尽量不要进入测试体(test.skip,或在套件前跳过)。Jest 没有测试体内的 test.skip(reason) — 请使用本包的 skip:

import { test } from '@jest/globals';
import { annotations, skip } from '@tracera/jest';

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

步骤

使用 tracera.step。来自 @jest/globals 的标准 beforeEach / afterEach 已仪器化:其中的步骤进入 准备 / 清理。

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

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 () => {
    // …
  });
});

注释、错误与附件

在步骤内是该步骤的字段。在步骤外是结果上的字段。文件请使用 tracera.attachment(结果最多 10 个附件,每个步骤最多 10 个)。

import { test } from '@jest/globals';
import { tracera } from '@tracera/jest';

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'),
    });
  });
});