globals

  • 类型: boolean
  • 默认值: false

为测试文件提供全局的 Rstest API,如 expecttestdescribe 等。

默认情况下,Rstest 不提供全局 API。如果你想像使用 Jest 一样全局使用这些 API,可以在配置中添加 globals: true或者在命令行中传入 --globals 选项。

rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  globals: true,
});

使用

当你启用 globals 后,就可以直接使用全局 API,而无需 import from '@rstest/core'

index.test.ts
- import { describe, expect, it } from '@rstest/core';

describe('Index', () => {
  it('should add two numbers correctly', () => {
    expect(1 + 1).toBe(2);
  });
});

类型支持

为了让 TypeScript 支持全局 API,请在 tsconfig.json 的 types 字段中添加 @rstest/core/globals

tsconfig.json
{
  "compilerOptions": {
    "types": ["@rstest/core/globals"]
  }
}