Using Rstest
Rstest is a test framework based on Rspack that provides comprehensive, first-class support for the Rspack ecosystem, you can use the same set of configurations for both development and testing.
Using Rstest will bring a seamless testing experience to your Rslib project.
Quick start
New project
You can use Rslib to create a new Rslib + Rstest project. Just add the --tools rstest flag when creating the project:
Existing project
To add Rstest to an existing Rslib project, simply install the @rstest/core package as a development dependency and add the Rstest command to the npm scripts in your package.json:
After completing the above steps, you can create test files in your project, write tests using the Rstest API, and run the tests with the npm test command.
For example, create a sum.test.ts file with the following content:
Now, you can run the npm test command to execute the tests. Rstest will output the following:
Configuring Rstest
Rstest provides various configuration options that can be set by creating an rstest.config.ts file in the root of your project. Here is an example configuration file:
In the configuration file, you can specify test file matching patterns, set the test environment, configure code coverage, etc. For detailed information on all available configuration options, please refer to the Rstest documentation.
Reusing Rslib configuration
@rstest/adapter-rslib is the official adapter provided by Rstest that allows Rstest to automatically inherit the configuration from your existing Rslib configuration file. This ensures that the test environment is consistent with the build configuration, avoiding duplicate configuration.
For more information about the withRslibConfig function, please refer to the Rstest documentation.
