close
logo
Rstest
指南
配置
API
English
简体中文
指南
配置
API
English
简体中文
logo
Rstest
Overview

Test API

Expect
Test
Describe
Hooks

Rstest Utility

Mock modules
Mock functions
MockInstance
Fake timers
Utilities
📝 在 GitHub 上编辑此页
上一页Test
下一页Hooks

#Describe

describe 用于定义测试套件(test suite),支持链式修饰符和参数化方法,便于灵活有序地组织测试。

#describe

  • 类型: (name: string, fn: () => void | Promise<void>) => void

定义一个测试套件,可以包含多个测试用例或嵌套的 describe。

import { describe, test } from '@rstest/core';

describe('math', () => {
  test('add', () => {
    // ...
  });
  test('sub', () => {
    // ...
  });
});

#describe.only

只运行被 only 标记的 describe 块。

describe.only('只运行这个套件', () => {
  // ...
});

#describe.skip

跳过被 skip 标记的 describe 块里的测试用例。

describe.skip('跳过这个套件里的测试用例', () => {
  // ...
});

需要注意的是,skip 标记仅用于跳过测试用例,describe 块内的代码仍会执行。这是因为 Rstest 需要收集测试用例的信息,以确保所有功能正常运行,即使它们被标记为跳过。例如,在快照测试中判断快照是过时还是被标记为 skipped。

describe.skip('a', () => {
  console.log('will run');
  test('b', () => {
    console.log('will not run');
    expect(0).toBe(0);
  });
});

#describe.todo

标记为待办的 describe 块。

describe.todo('should implement this suite');

#describe.each

  • 类型: describe.each(cases: ReadonlyArray<T>)(name: string, fn: (param: T) => void | Promise<void>) => void

为数组中的每一项生成一个 describe 块。

describe.each([
  { a: 1, b: 2 },
  { a: 2, b: 3 },
])('math $a + $b', ({ a, b }) => {
  test('add', () => {
    // ...
  });
});

#describe.for

  • 类型: describe.for(cases: ReadonlyArray<T>)(name: string, fn: (param: T) => void | Promise<void>) => void

类似 each,但参数类型更灵活。

describe.for([
  [1, 2],
  [2, 3],
])('math $0 + $1', ([a, b]) => {
  test('add', () => {
    // ...
  });
});

#describe.runIf

仅当条件为 true 时运行该 describe 块。

describe.runIf(process.env.RUN_EXTRA === '1')('有条件地运行', () => {
  // ...
});

#describe.skipIf

当条件为 true 时跳过该 describe 块。

describe.skipIf(process.platform === 'win32')('Windows 下跳过', () => {
  // ...
});

#describe.concurrent

并发运行该 describe 下的测试。

describe.concurrent('并发套件', () => {
  test('test 1', async () => {
    /* ... */
  });
  test('test 2', async () => {
    /* ... */
  });
});

#describe.sequential

顺序运行该 describe 下的测试(默认行为)。

describe.sequential('顺序套件', () => {
  test('test 1', async () => {
    /* ... */
  });
  test('test 2', async () => {
    /* ... */
  });
});