Rstest instance
Everything on this page is exported from the @rstest/core/api entry of @rstest/core. Use these APIs to create Rstest instances, run or list tests, start watch sessions, and merge blob reports.
All exports are currently experimental and may change before Rstest 1.0.0. Pin @rstest/core to an exact version for now to ensure API stability.
createRstest
The createRstest function creates and returns an Rstest instance. It resolves config once during instance creation and reuses it as the instance's base config. Options passed to instance methods apply to the current operation without mutating the base config.
cwd defaults to process.cwd() and is the base for resolving a relative config.root; omitting config uses an empty inline config and does not discover a config file in cwd.
createRstest sets RSTEST=true, sets NODE_ENV=test only when it is unset, and never restores either environment variable.
Example
Load a config file
Use loadConfig from the main entry to load a config file, then pass its return value ({ content, filePath }) directly as config.
CreateRstestOptions
- Type:
rstest.context
rstest.context is a read-only object resolved once when the instance is created. You can inspect the resolved state without running tests: use rootPath to map testPath values from results back to the workspace, projects to render or filter a multi-project setup, config to inspect the effective configuration, and version for compatibility checks.
Example
RstestContext
- Type:
context.version
The version of @rstest/core currently in use.
- Type:
string
context.rootPath
The absolute root path of the Rstest instance. It is resolved from config.root, using cwd as the base for a relative value.
- Type:
string
context.config
The normalized config of the Rstest instance.
- Type:
Readonly<NormalizedConfig>
context.projects
The resolved project contexts. Without an explicit projects config, the array contains the default project named by config.name ('rstest' by default).
- Type:
readonly ProjectContext[]
rstest.run
rstest.run() runs one test cycle and returns a TestRunResult. Failures are reported through status and never reject the promise; see TestRunResult.
Example
Select test files
The following options select test files:
filtersuses case-insensitive substring matching by default. Omitfiltersto select all files; an explicit empty array selects none in either mode.filterModeuses normalized path equality when set to'exact'.relatedinterpretsfiltersas source files and selects their related tests.changedselects tests related to working tree changes, optionally since a Git ref.changed: falsedisables changed-file selection.shardaccepts the CLI-style"1/3"form or{ index: 1, count: 3 }.projectsupports project names,*wildcards, and!exclusions.
The remaining options control execution behavior for this run:
testNamePatternselects tests by name using aRegExpor string pattern.updatecontrols the snapshot update mode.update: falseexplicitly disables snapshot updates; omittingupdatekeeps the configured or default mode.bailis the number of test failures allowed before stopping this run; booleantruemeans one.passWithNoTestscontrols whether this run succeeds when no tests match.
RunOptions
- Type:
TestRunResult
- Type:
status is 'error' when unhandledErrors is non-empty. It is 'fail' when the run completed with a non-zero exit status, such as from failing tests or test files, a coverage threshold violation, no tests being found without passWithNoTests, or a globalSetup teardown failure. Otherwise, it is 'pass'.
snapshot is absent only when execution stops before a run begins. coverage is present when coverage is enabled.
SerializedError values are plain JSON-safe objects, not error class instances.
rstest.watch
rstest.watch() starts a Node watch session and returns a watcher that closes it.
Example
onResult runs after every completed cycle, including the initial cycle. Its result is cycle-scoped: files and summary contain only files that actually ran in that cycle. To maintain session state, merge results by testPath yourself.
Errors thrown by onResult are isolated and do not stop the watch session. Calling watcher.close() releases the compiler, workers, file watchers, and pending globalSetup teardown. It is idempotent, repeated calls observe the same result, and it rejects if teardown fails.
rstest.watch() rejects related and changed because they select a fixed file set, while a watch session must pick up newly related tests. Use rstest.run() for these options. related: false and changed: false are ignored.
Browser projects are not supported by rstest.watch() yet, so startup rejects. Use rstest.run() for browser projects.
WatchOptions
- Type:
rstest.listTests
rstest.listTests() collects test files and test declarations without running test bodies.
Example
Set filesOnly to skip collecting declarations. includeSuites includes named suites as separate entries. includeLocation adds source locations.
Every entry includes testPath and project. Entries from the implicit default project use config.name ('rstest' by default). Declaration entries include their own name, a suite-prefixed fullName, and parentNames for the hierarchy; file entries omit name and fullName. Skipped and todo declarations are included with runMode set to skip or todo; runnable declarations omit runMode.
Entries are returned in depth-first declaration order. A suite immediately precedes its descendants, and all entries from one file stay contiguous.
rstest.listTests() rejects when collection fails instead of returning a partial or empty list.
rstest.listTests() ignores shard and always lists every file, unlike rstest list --shard, which lists only the selected shard.
ListOptions
- Type:
rstest.mergeReports
rstest.mergeReports() merges blob reports and returns the same result model as rstest.run().
Example
path selects the blob-report directory. cleanup removes consumed reports after a successful merge.
MergeReportsOptions
- Type:
runCLI
runCLI runs the Rstest command line in the current process. It parses argv, sets process.exitCode, and installs the CLI's signal handling. Use it when you want the CLI's process behavior; instance methods do not set process.exitCode or install signal handlers.
Example
argv contains the command, filters, and flags exactly as you would type them after rstest on the command line. It defaults to process.argv.slice(2).
See the CLI documentation for all available commands and flags.
RunCLIOptions
- Type: