Rspack
This guide covers how to integrate Rstest with Rspack for seamless testing in your Rspack projects.
Quick start
To add Rstest to an existing Rspack project, follow the Quick Start to install and set up test scripts.
Reuse Rspack config
@rstest/adapter-rspack is an official adapter that allows Rstest to automatically inherit configuration from your existing Rspack config file. This ensures your test environment matches your build configuration without duplication.
Install adapter
Extend your config
Using the withRspackConfig function from the adapter, you can extend your Rstest configuration from the Rspack config file.
This will automatically:
- Load your
rspack.config.tsfile - Map compatible Rspack options to Rstest configuration
- Merge with any additional Rstest config you provide
By default, the adapter uses process.cwd() to resolve the Rspack config. If your config lives elsewhere, you can use the cwd option. See API for more details.
Since Rstest uses Rsbuild internally as its bundler, some Rstest built-in behaviors may differ from Rspack's defaults. For example, Rstest has its own CSS processing pipeline. To avoid conflicts, this adapter automatically disables Rstest (Rsbuild) built-in CSS plugins so that Rspack's CSS configuration takes effect. This means that some Rsbuild-specific CSS features and configurations (e.g., output.cssModules configuration) will not be available when using this adapter.
API
withRspackConfig(options)
Returns a configuration function that loads Rspack config and converts it to Rstest configuration.
cwd
- Type:
string - Default:
process.cwd()
The working directory to resolve the Rspack config file.
When your Rspack config is in a different directory or you are running tests in a monorepo (where your process.cwd() is not your config directory), you can specify the cwd option to resolve the Rspack config file from a different directory.
configPath
- Type:
string - Default:
'./rspack.config.ts'
Path to rspack config file.
configName
- Type:
string - Default:
undefined
Select a named configuration when using multi-config in your Rspack config file. Set to a string to use the config with a matching name field.
If your Rspack config exports an array of configurations:
You can select a specific configuration in your Rstest config:
When you need to test multiple parts of your application with different configurations independently, you can define multiple Rstest projects:
env
- Type:
Record<string, unknown> | string[] - Default:
undefined
Environment values passed to the Rspack config function. This corresponds to the env parameter in rspack.config.ts when exporting a function:
nodeEnv
- Type:
string - Default:
undefined
The NODE_ENV value used when loading the Rspack config.
modifyRspackConfig
- Type:
(config: RspackOptions) => RspackOptions - Default:
undefined
Modify the Rspack config before it gets converted to Rstest config:
Configuration mapping
The adapter automatically maps these Rspack options to Rstest:
The following Rspack options are not mapped and will be ignored:
Debug config
To see the resolved configuration returned by the adapter, wrap it and log the result: