close
logo
Rstest
Guide
Config
API
English
简体中文
Guide
Config
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
📝 Edit this page on GitHub
Previous PagepassWithNoTests
Next PagetestNamePattern

#includeSource

  • Type: string[]
  • Default: []

In-source testing is where the test code lives within the same file as the source code, similar to Rust's module tests.

You can define a list of glob patterns that match your in-source test files via includeSource configuration.

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

export default defineConfig({
  includeSource: ['src/**/*.{js,ts}'],
});
TIP

In-source testing is usually suitable for small functional functions and utilities, allowing for easy and rapid verification and debugging. For more complex functions and modules, independent test files are recommended.

#Writing in-source tests

When includeSource defined, Rstest will run all matched files with import.meta.rstest inside.

You can get the Rstest test API via import.meta.rstest.

src/helper.ts
export const sayHi = () => 'hi';

if (import.meta.rstest) {
  const { it, expect } = import.meta.rstest;
  it('should test source code correctly', () => {
    expect(sayHi()).toBe('hi');
  });
}

#For production

Put the test code inside the if (import.meta.rstest) block, and define import.meta.rstest as false in your build configuration (e.g., rsbuild.config.ts), which will help the bundler eliminate dead code.

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

export default defineConfig({
  source: {
    define: {
+      'import.meta.rstest': false,
    },
  },
});

#TypeScript

To get TypeScript support for import.meta.rstest, you can create a src/rstestEnv.d.ts file to reference:

rstestEnv.d.ts
/// <reference types="@rstest/core/importMeta" />