close

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:

npx create-rslib --template react-ts --tools rstest --dir my-project

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 configuration from your existing Rslib config file. This ensures your test environment matches your build configuration without duplication.

Install adapter

npm
yarn
pnpm
bun
deno
npm add @rstest/adapter-rslib -D

Extend your config

Using the withRslibConfig function from the adapter, you can extend your Rstest configuration from the Rslib config file.

import { defineConfig } from '@rstest/core';
import { withRslibConfig } from '@rstest/adapter-rslib';

export default defineConfig({
  extends: withRslibConfig(),
  // Additional rstest-specific configuration
});

This will automatically:

  • Load your rslib.config.ts file
  • Map compatible Rslib options to Rstest configuration
  • Merge with any additional Rstest config you provide

By default, the adapter uses process.cwd() to resolve the Rslib config. If your config lives elsewhere, set cwd:

export default defineConfig({
  extends: withRslibConfig({
    cwd: './packages/my-lib',
  }),
});

See the @rstest/adapter-rslib documentation for advanced options.

Configuration mapping

The adapter automatically maps these Rslib options to Rstest:

Rslib optionRstest equivalentNotes
lib.idnameLibrary identifier
pluginspluginsPlugin configuration
source.decoratorssource.decoratorsDecorator support
source.definesource.defineGlobal constants
source.includesource.includeSource inclusion patterns
source.excludesource.excludeSource exclusion patterns
source.tsconfigPathsource.tsconfigPathTypeScript config path
resolveresolveModule resolution
output.cssModulesoutput.cssModulesCSS modules configuration
tools.rspacktools.rspackRspack configuration
tools.swctools.swcSWC configuration
tools.bundlerChaintools.bundlerChainBundler chain configuration
output.targettestEnvironment'happy-dom' for web, 'node' for node and other targets

Debug config

To see the resolved configuration returned by the adapter, wrap it and log the result:

export default defineConfig({
  extends: async (user) => {
    const config = await withRslibConfig({ libId: 'react' })(user);
    console.log('Extended config:', JSON.stringify(config, null, 2));
    return config;
  },
});