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 上编辑此页
上一页isolate
下一页clearMocks

#testEnvironment

  • 类型: 'node' | 'jsdom' | 'happy-dom'
  • 默认值: 'node'
  • CLI: --testEnvironment=node

测试时所使用的环境。

Rstest 默认使用 Node.js 作为测试环境。如果你在开发 Web 应用,可以使用类浏览器环境,如 jsdom 或 happy-dom。

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

export default defineConfig({
  testEnvironment: 'jsdom',
});

#DOM 测试

Rstest 支持使用 jsdom 和 happy-dom 来模拟 DOM 和浏览器 API。

如果你想启用 DOM 测试,可以使用如下配置:

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

export default defineConfig({
  testEnvironment: 'jsdom', // 或 'happy-dom'
});

你还需要安装对应的包:

使用 jsdom

npm
yarn
pnpm
bun
npm add jsdom -D

使用 happy-dom

npm
yarn
pnpm
bun
npm add happy-dom -D

启用 DOM 测试后,你可以在测试用例中使用 document 和 window 等浏览器 API。

test('DOM test', () => {
  document.body.innerHTML = '<p class="content">hello world</p>';
  const paragraph = document.querySelector('.content');
  expect(paragraph?.innerHTML).toBe('hello world');
});

#示例

  • Rstest + node
  • Rstest + jsdom + react