CLI

Rstest comes with a lightweight CLI that includes commands such as rstest watch and rstest run.

rstest -h

rstest -h can help you view all available CLI commands and options:

npx rstest -h

The output is shown below:

Usage:
  $ rstest [...filters]

Commands:
  [...filters]        run tests
  run [...filters]    run tests without watch mode
  watch [...filters]  run tests in watch mode
  list [...filters]   lists all test files that Rstest will run

Options:
  -h, --help                               Display this message
  -v, --version                            Display version number
  -c, --config <config>
  ...

rstest [...filters]

Running rstest directly will enable the Rstest test in the current directory. Listening mode is automatically entered in the development environment (equivalent to rstest watch), while a single test is performed in the CI environment or non-terminal interactive mode (equivalent to rstest run).

$ npx rstest

 test/index.test.ts (2 tests) 1ms

  Test Files 1 passed (1)
       Tests 2 passed (2)
    Duration 189 ms (build 22 ms, tests 167 ms)

rstest run

rstest run will perform a single run, and the command is suitable for CI environments or scenarios where tests are not required to be performed while modifying.

rstest watch

rstest watch will start listening mode and execute tests, and when the test or dependent file modifications, the associated test file will be re-execute.

rstest list

rstest list will print a test list of all matching conditions. By default, it prints the test names of all matching tests.

$ npx rstest list

# the output is shown below:
a.test.ts > test a > test a-1
a.test.ts > test a-2
b.test.ts > test b > test b-1
b.test.ts > test b-2

The rstest list command inherits all rstest filtering options, you can filter files directly or use -t to filter the specified test name.

$ npx rstest list -t='test a'

# the output is shown below:
a.test.ts > test a > test a-1
a.test.ts > test a-2

You can use --filesOnly to make it print the test files only:

$ npx rstest list --filesOnly

# the output is shown below:
a.test.ts
b.test.ts

You can use --json to make it print tests in JSON format in terminal or save the results to a separate file:

$ npx rstest list --json

$ npx rstest list --json=./output.json

CLI options

Rstest CLI provides several common options that can be used with all commands:

FlagDescription
-c, --config <config>Specify the configuration file, can be a relative or absolute path, see Specify config file
--config-loader <loader>Specify the config loader, can be jiti or native, see Rsbuild - Specify config loader
-r, --root <root>Specify the project root directory, see root
--globalsProvide global APIs, see globals
--isolateRun tests in an isolated environment, see isolate
--exclude <exclude>Exclude files from test, see exclude
-u, --updateUpdate snapshot files, see update
--passWithNoTestsAllows the test suite to pass when no files are found, see passWithNoTests
--printConsoleTracePrint console traces when calling any console method, see printConsoleTrace
--disableConsoleInterceptDisable console intercept, see disableConsoleIntercept
--slowTestThreshold <value>The number of milliseconds after which a test or suite is considered slow, see slowTestThreshold
-t, --testNamePattern <value>Run only tests with a name that matches the regex, see testNamePattern
--testEnvironment <name>The environment that will be used for testing, see testEnvironment
--testTimeout <value>Timeout of a test in milliseconds, see testTimeout
--hookTimeout <value>Timeout of hook in milliseconds, see hookTimeout
--retry <retry>Number of times to retry a test if it fails, see retry
--maxConcurrency <value>Maximum number of concurrent tests, see maxConcurrency
--clearMocksAutomatically clear mock calls, instances, contexts and results before every test, see clearMocks
--resetMocksAutomatically reset mock state before every test, see resetMocks
--restoreMocksAutomatically restore mock state and implementation before every test, see restoreMocks
--unstubGlobalsRestores all global variables that were changed with rstest.stubGlobal before every test, see unstubGlobals
--unstubEnvsRestores all process.env values that were changed with rstest.stubEnv before every test, see unstubEnvs
-h, --helpDisplay help for command
-v, --versionDisplay version