Adapters
Adapters are a powerful feature that allows you to convert configurations from other tools (such as build tools or CLIs) into a format that Rstest supports. By creating a custom adapter, you can reuse existing configurations, avoid redundant definitions across multiple tools, and ensure project consistency.
This guide will walk you through the process of creating and using custom Rstest adapters.
Why use adapters
- Reuse Configuration: Convert existing build tool configurations (e.g., path aliases, global variables) into test configurations to avoid redundant definitions.
- Framework Customization: Allow frameworks or templates to preset test configurations, such as adding specific
setupscripts, changing default timeouts, or setting default test environments, thereby providing an out-of-the-box testing experience. - Simplify Maintenance: Centralize configuration management, reduce the hassle of manually synchronizing configurations across different tools, and lower the chance of errors.
- Quick Integration: Through adapters, you can quickly integrate Rstest into existing projects and reuse existing project configurations.
Core concepts
Rstest's configuration extension functionality primarily relies on the extends option. This option can accept an object or a function (ExtendConfigFn) that returns a configuration object (ExtendConfig) or a Promise resolving to such an object.
An adapter is essentially a function that implements the ExtendConfigFn interface.
The adapter function receives the user's current Rstest configuration as an argument and returns a new configuration object, which will be deeply merged with the user's configuration.
Creating a custom adapter
Creating a custom adapter involves the following steps:
- Define an adapter function: This function should conform to the
ExtendConfigFntype signature. - Load external configuration: Inside the function, read and parse the configuration file of the tool you want to integrate (e.g.,
my-build-tool.config.js). - Transform configuration: Map the loaded configuration to the fields defined in the
ExtendConfiginterface. This is the core logic of the adapter, where you decide how to convert properties from the source configuration (e.g.,alias,define) to the corresponding Rstest options (e.g.,resolve.alias,source.define). - Return configuration: Return the transformed
ExtendConfigobject.
Example: A simple adapter
Suppose you have a custom build configuration file my.config.ts with the following content:
You can create an adapter my-adapter.ts to read this file and convert it into an Rstest configuration.
Using your custom adapter
Now, you can use the adapter you created in your Rstest configuration file:
When Rstest loads this configuration, it will:
- Call the
withMyConfigfunction. - Get the returned configuration object.
- Deeply merge it with the inline configuration in
defineConfig. Note that the user's local configuration (e.g.,testTimeout: 8000) will take precedence over the configuration returned byextends.
Advanced usage: adapters with options
Similar to @rstest/adapter-rslib, you can allow your adapter to accept an options object for more flexible integration.
Usage example:
In this way, you can create powerful and reusable adapters that seamlessly integrate any toolchain with Rstest.
Using official adapters
The following are the officially supported Rstest adapters:
- Rslib Adapter: Used to extend Rstest configuration from Rslib configuration.