Rslib
This guide covers how to integrate Rstest with Rslib for seamless testing in your Rslib projects.
Quick start
New project
Create a new Rslib + Rstest project. Add the --tools rstest flag when creating:
The scaffold includes Rstest and demo tests. Run them with npm run test.
Existing project
To add Rstest to an existing project, follow the Quick Start to install and set up test scripts.
Reuse Rslib config
@rstest/adapter-rslib is an official adapter that allows Rstest to automatically inherit test-relevant configuration from your existing Rslib config file. This keeps your test environment aligned with your build setup without carrying over options that only matter to build output.
Install adapter
Extend your config
Using the withRslibConfig function from the adapter, you can extend your Rstest configuration from the Rslib config file.
This will automatically:
- Load your
rslib.config.tsfile - Extract and map test-relevant Rslib options to Rstest configuration
- Merge with any additional Rstest config you provide
The adapter does not reuse the entire Rslib config as-is. It keeps the parts that matter for test execution, such as module resolution, transforms, CSS Modules, decorator-related settings, static asset handling, and target, and automatically drops unmapped fields like dev, server, and html, along with other options that only exist for dev server, page entry, or build output.
By default, the adapter uses process.cwd() to resolve the Rslib config. If your config lives elsewhere, you can use the cwd option. See API for more details.
API
withRslibConfig(options)
Returns a configuration function that loads Rslib config and converts it to Rstest configuration.
cwd
- Type:
string - Default:
process.cwd()
The cwd is the working directory to resolve the Rslib config file.
When your Rslib 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 Rslib config file from a different directory.
configPath
- Type:
string - Default:
'./rslib.config.ts'
Path to rslib config file.
libId
- Type:
string - Default:
undefined
The lib config id in lib field to use. Set to a string to use the lib config with matching id.
By default, the adapter uses the common configuration from Rslib. If your Rslib config has multiple lib configurations:
You can then reference specific lib configurations in your Rstest config. Rstest will adapt the Rslib shared configuration and the lib configuration with a matching libId to Rstest format.
This still does not copy the whole lib config verbatim. The adapter only takes the lib's source, output, tools, plugins, and resolve fields that are relevant to test execution, then merges them with the shared config before converting the result to Rstest format.
When you need to test multiple parts of your application with different configurations independently, you can define multiple Rstest projects. Each project can extend a specific lib configuration by setting the libId option.
modifyLibConfig
- Type:
(config: RslibConfig) => RslibConfig | void - Default:
undefined
Modify the Rslib config before it gets converted to Rstest config:
Configuration mapping
The adapter automatically maps these Rslib options to Rstest:
Only the fields listed below are inherited. Rslib options that are not listed are ignored by default, which means test-irrelevant sections such as dev, server, and html are automatically pruned during conversion.
Debug config
To see the resolved configuration returned by the adapter, wrap it and log the result: