--- url: /guide/advanced/debugging.md --- # Debugging ## Debug mode Rstest provides a debug mode to troubleshoot problems, you can add the `DEBUG=rstest` environment variable when testing to enable Rstest's debug mode. ```bash DEBUG=rstest pnpm test ``` In debug mode, Rstest will: - Write test temporary outputs to disk - Write the Rstest config, Rsbuild config and Rspack config to the dist directory - Show full error stack traces - Set logger level to `verbose` ### Rstest config file In debug mode, Rstest will automatically generate `dist/.rsbuild/rstest.config.mjs` file, which contains the final generated Rstest config. In this file, you can know the final result of the Rstest config you passed in after being processed by the framework and Rstest. The content of the file is as follows: ```js title="rstest.config.mjs" export default { name: 'rstest', include: ['**/*.{test,spec}.?(c|m)[jt]s?(x)'], exclude: [ '**/node_modules/**', '**/dist/**', '**/.{idea,git,cache,output,temp}/**', '**/dist/.rstest-temp', ], includeSource: [], pool: { type: 'forks', }, isolate: true, globals: false, passWithNoTests: false, update: false, testTimeout: 5000, testEnvironment: 'node', retry: 0, clearMocks: false, resetMocks: false, restoreMocks: false, slowTestThreshold: 300, // other configs... }; ``` For a complete introduction to Rstest config, please see the [Configure Rstest](/guide/basic/configure-rstest.md) chapter. ## Debugging in VS Code Rstest supports debugging in VS Code using `debugger` statements or breakpoints. Just open the `JavaScript Debug Terminal` and run the test command in the terminal. You can also add a dedicated launch configuration to debug a test file in VS Code: ```json title=".vscode/launch.json" { "version": "0.2.0", "configurations": [ { "name": "Debug Current Test File", "type": "node", "request": "launch", "program": "${workspaceFolder}/node_modules/@rstest/core/bin/rstest.js", "args": ["run", "${file}"], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", "skipFiles": ["/**"] } ] } ``` Then you can start debugging the current test file directly in VS Code by pressing `F5` or go to the `Run and Debug` panel. --- url: /guide/advanced/profiling.md --- # Profiling ## Using Rsdoctor [Rsdoctor](https://rsdoctor.rs/) is a build analysis tool that can visually display the compilation time of each loaders and plugins. When you need to debug Rstest's build outputs or build processes, you can use Rsdoctor for troubleshooting. ### Quick start In Rstest, you can enable Rsdoctor analysis as follows: 1. Install the Rsdoctor plugin: ```sh [npm] npm add @rsdoctor/rspack-plugin -D ``` ```sh [yarn] yarn add @rsdoctor/rspack-plugin -D ``` ```sh [pnpm] pnpm add @rsdoctor/rspack-plugin -D ``` ```sh [bun] bun add @rsdoctor/rspack-plugin -D ``` ```sh [deno] deno add npm:@rsdoctor/rspack-plugin -D ``` 2. Add `RSDOCTOR=true` env variable before the CLI command: ```json title="package.json" { "scripts": { "test:rsdoctor": "RSDOCTOR=true rstest run" } } ``` As Windows does not support the above usage, you can also use [cross-env](https://npmjs.com/package/cross-env) to set environment variables. This ensures compatibility across different systems: ```json title="package.json" { "scripts": { "test:rsdoctor": "cross-env RSDOCTOR=true rstest run" }, "devDependencies": { "cross-env": "^7.0.0" } } ``` After running the above commands, Rstest will automatically register the Rsdoctor plugin, and after the build is completed, it will open the build analysis page. For complete features, please refer to [Rsdoctor document](https://rsdoctor.rs/). ![rsdoctor-rstest-outputs](https://assets.rspack.rs/rstest/assets/rsdoctor-rstest-outputs.png) ## Using samply > Note: In order to be able to profiling the Node.js side code in macOS, Node.js v22.16+ is required. [Samply](https://github.com/mstange/samply) supports performance analysis for both Rstest main process and test process simultaneously. You can perform a complete performance analysis through the following steps: Run the following command to start performance analysis:: ```bash samply record -- node --perf-prof --perf-basic-prof --interpreted-frames-native-stack {your_node_modules_folder}/@rstest/core/bin/rstest.js ``` After the command execution, the analysis results will automatically open in the [Firefox Profiler](https://profiler.firefox.com/). Rstest’s JavaScript typically runs in the Node.js thread. Select the Node.js thread to view the time distribution on the Node.js side. ![rstest-samply-profiling](https://assets.rspack.rs/rstest/assets/rstest-samply-profiling.png) ## Node.js profiling You can also use Node.js built-in profiling tools to analyze Rstest's performance. For example, you can use the `--heap-prof` flag to enable heap profiling: ```bash node --heap-prof --heap-prof-dir=./heap-prof ./node_modules/@rstest/core/bin/rstest.js ``` The heap profile files will be generated in the `./heap-prof` directory. You can analyze these files using tools like [Visual Studio Code](https://code.visualstudio.com/docs/nodejs/profiling#_analyzing-a-profile) or [Chrome DevTools](https://developer.chrome.com/docs/devtools/memory-problems/heap-snapshots). You can also use the `--inspect` flag to enable [Node.js inspector](https://nodejs.org/en/learn/getting-started/debugging). ```bash node --inspect ./node_modules/@rstest/core/bin/rstest.js watch ``` --- url: /guide/basic/cli.md --- # CLI Rstest comes with a lightweight CLI that includes commands such as [rstest watch](#rstest-watch) and [rstest run](#rstest-run). ## rstest -h `rstest -h` can help you view all available CLI commands and options: ```bash npx rstest -h ``` The output is shown below: ```bash 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: -w, --watch Run tests in watch mode -h, --help Display this message -v, --version Display version number -c, --config ... ``` ## rstest \[...filters] Running `rstest` directly will enable the Rstest test in the current directory. ```bash $ 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) ``` ### Watch mode If you want to automatically rerun the test when the file changes, you can use the `--watch` flag or `rstest watch` command: ```bash $ npx rstest --watch ``` ## 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. ```bash $ 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. ```bash $ 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: ```bash $ 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: ```bash $ npx rstest list --json $ npx rstest list --json=./output.json ``` You can use `--includeSuites` to print test suites along side test cases: ```bash $ npx rstest list # the output is shown below: a.test.ts > test a a.test.ts > test a > test a-1 a.test.ts > test a-2 b.test.ts > test b b.test.ts > test b > test b-1 b.test.ts > test b-2 ``` You can use `--printLocation` to print location of tests: ```bash $ npx rstest list # the output is shown below: a.test.ts:4:5 > test a > test a-1 a.test.ts:9:3 > test a-2 b.test.ts:4:5 > test b > test b-1 b.test.ts:9:3 > test b-2 ``` ## CLI options Rstest CLI provides several common options that can be used with all commands: | Flag | Description | | ------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `-c, --config ` | Specify the configuration file, can be a relative or absolute path, see [Specify config file](/guide/basic/configure-rstest.md#specify-config-file) | | `--config-loader ` | Specify the config loader (`auto` \| `jiti` \| `native`), see [Rsbuild - Specify config loader](https://rsbuild.rs/guide/configuration/rsbuild#specify-config-loader) | | `-r, --root ` | Specify the project root directory, see [root](/config/test/root.md) | | `--globals` | Provide global APIs, see [globals](/config/test/globals.md) | | `--isolate` | Run tests in an isolated environment, see [isolate](/config/test/isolate.md) | | `--reporter ` | Specify the test reporter, see [reporters](/config/test/reporters.md) | | `--exclude ` | Exclude files from test, see [exclude](/config/test/exclude.md) | | `-u, --update` | Update snapshot files, see [update](/config/test/update.md) | | `--coverage` | Enable code coverage collection, see [coverage](/config/test/coverage.md) | | `--passWithNoTests` | Allows the test suite to pass when no files are found, see [passWithNoTests](/config/test/pass-with-no-tests.md) | | `--printConsoleTrace` | Print console traces when calling any console method, see [printConsoleTrace](/config/test/print-console-trace.md) | | `--project ` | Only run tests for the specified project, see [Filter by project name](/guide/basic/test-filter.md#filter-by-project-name) | | `--disableConsoleIntercept` | Disable console intercept, see [disableConsoleIntercept](/config/test/disable-console-intercept.md) | | `--slowTestThreshold ` | The number of milliseconds after which a test or suite is considered slow, see [slowTestThreshold](/config/test/slow-test-threshold.md) | | `-t, --testNamePattern ` | Run only tests with a name that matches the regex, see [testNamePattern](/config/test/test-name-pattern.md) | | `--testEnvironment ` | The environment that will be used for testing, see [testEnvironment](/config/test/test-environment.md) | | `--testTimeout ` | Timeout of a test in milliseconds, see [testTimeout](/config/test/test-timeout.md) | | `--hookTimeout ` | Timeout of hook in milliseconds, see [hookTimeout](/config/test/hook-timeout.md) | | `--retry ` | Number of times to retry a test if it fails, see [retry](/config/test/retry.md) | | `--bail [number]` | Abort the test run after the specified number of test failures, see [bail](/config/test/bail.md) | | `--browser, --browser.enabled` | Run tests in Browser Mode, see [browser](/config/test/browser.md) | | `--browser.name ` | Browser to use: `chromium`, `firefox`, `webkit` (default: `chromium`), see [browser](/config/test/browser.md) | | `--browser.headless` | Run browser in headless mode (default: `true` in CI), see [browser](/config/test/browser.md) | | `--browser.port ` | Port for the Browser Mode dev server, see [browser](/config/test/browser.md) | | `--browser.strictPort` | Exit if the specified port is already in use, see [browser](/config/test/browser.md) | | `--maxConcurrency ` | Maximum number of concurrent tests, see [maxConcurrency](/config/test/max-concurrency.md) | | `--clearMocks` | Automatically clear mock calls, instances, contexts and results before every test, see [clearMocks](/config/test/clear-mocks.md) | | `--resetMocks` | Automatically reset mock state before every test, see [resetMocks](/config/test/reset-mocks.md) | | `--restoreMocks` | Automatically restore mock state and implementation before every test, see [restoreMocks](/config/test/restore-mocks.md) | | `--unstubGlobals` | Restores all global variables that were changed with `rstest.stubGlobal` before every test, see [unstubGlobals](/config/test/unstub-globals.md) | | `--unstubEnvs` | Restores all `process.env` values that were changed with `rstest.stubEnv` before every test, see [unstubEnvs](/config/test/unstub-envs.md) | | `--include ` | Specify test file matching pattern, see [include](/config/test/include.md) | | `--includeSource ` | Specify in-source testing file matching pattern, see [includeSource](/config/test/include-source.md) | | `--logHeapUsage` | Print heap usage for each test, see [logHeapUsage](/config/test/log-heap-usage.md) | | `--hideSkippedTests` | Do not display skipped test logs, see [hideSkippedTests](/config/test/hide-skipped-tests.md) | | `--hideSkippedTestFiles` | Do not display skipped test file logs, see [hideSkippedTestFiles](/config/test/hide-skipped-test-files.md) | | `--chaiConfig ` | Customize Chai configuration, see [chaiConfig](/config/test/chai-config.md) | | `--env ` | Set environment variables, see [env](/config/test/env.md) | | `--pool ` | Shorthand for `--pool.type`, see [pool](/config/test/pool.md) | | `--pool.type ` | Specify the test pool type, see [pool](/config/test/pool.md) | | `--pool.maxWorkers ` | Maximum number or percentage of workers, see [pool](/config/test/pool.md) | | `--pool.minWorkers ` | Minimum number or percentage of workers, see [pool](/config/test/pool.md) | | `--pool.execArgv ` | Additional Node.js execArgv for worker processes (repeatable), see [pool](/config/test/pool.md) | | `--setupFiles ` | Specify setup files list, see [setupFiles](/config/test/setup-files.md) | | `--snapshotFormat ` | Customize snapshot format, see [snapshotFormat](/config/test/snapshot-format.md) | | `--resolveSnapshotPath` | Customize snapshot path resolution, see [resolveSnapshotPath](/config/test/resolve-snapshot-path.md) | | `--onConsoleLog ` | Handle console logs, see [onConsoleLog](/config/test/on-console-log.md) | | `--name ` | Set test suite name, see [name](/config/test/name.md) | | `--projects ` | Configure multi-project testing, see [projects](/config/test/projects.md) | | `-h, --help` | Display help for command | | `-v, --version` | Display version | ### Boolean option negation For boolean options, you can use the `--no-