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

Test Configurations

root
name
include
exclude
setupFiles
projects
update
globals
passWithNoTests
includeSource
testNamePattern
env
retry
testTimeout
hookTimeout
maxConcurrency
pool
isolate
testEnvironment
clearMocks
resetMocks
restoreMocks
unstubEnvs
unstubGlobals
coverage
reporters
hideSkippedTests
slowTestThreshold
snapshotFormat
resolveSnapshotPath
printConsoleTrace
onConsoleLog
disableConsoleIntercept

Build Configurations

plugins
source
output
resolve
tools
dev
performance
📝 在 GitHub 上编辑此页
上一页setupFiles
下一页update

#projects

  • 类型:
type ProjectConfig = Omit<
  RstestConfig,
  'projects' | 'reporters' | 'pool' | 'isolate' | 'coverage'
>;

type Projects = (string | ProjectConfig)[];
  • 默认值: [<rootDir>]

定义多个测试项目,可以是一个目录、配置文件或 glob 模式,也可以是一个对象。 Rstest 将会按照各个项目定义的配置运行对应的测试,所有项目的测试结果将会合并展示。

你可以通过 --project 选项来过滤运行特定项目。

如果没有 projects 字段,rstest 会将当前目录视为单个项目。

import { defineConfig } from '@rstest/core';

export default defineConfig({
  projects: [
    // A monorepo: each package directory is a project
    'packages/*',

    // All apps that provide an rstest config file
    'apps/**/rstest.config.ts',

    // A specific project directory
    '<rootDir>/services/auth',

    // A specific project's config file
    './projects/web/rstest.config.ts',

    // inline project configs
    {
      name: 'node',
      include: ['tests/node/**/*.{test,spec}.{js,cjs,mjs,ts,tsx}'],
    },
    {
      name: 'react',
      include: ['tests/react/**/*.{test,spec}.{js,cjs,mjs,ts,tsx}'],
      testEnvironment: 'jsdom',
    },
  ],
});

#配置说明

需要注意的是:

  • project 配置并不会继承全局配置,如果你的子项目间存在共享配置,可以抽取 shared 配置,并在子项目中引入
  • 一些全局配置,如 reporters、pool、isolate 等,在 project 配置中是无效的
  • 不支持嵌套的 projects
packages/pkg-a/rstest.config.ts
import { defineConfig } from '@rstest/core';
import sharedConfig from '../shared/rstest.config';

export default defineConfig({
  ...sharedConfig,
});

#内联配置

Rstest 支持在 projects 字段中直接通过内联的方式配置 project。这允许你在单个项目下定义多个测试项目,而无需为每个测试项目创建单独的配置文件。

import { defineConfig } from '@rstest/core';

export default defineConfig({
  projects: [
    // inline project configs
    {
      name: 'node',
      include: ['tests/node/**/*.{test,spec}.{js,cjs,mjs,ts,tsx}'],
    },
    {
      name: 'react',
      include: ['tests/react/**/*.{test,spec}.{js,cjs,mjs,ts,tsx}'],
      testEnvironment: 'jsdom',
    },
  ],
});