close
  • English
  • plugins pluginsplugins

    plugins is used to register Rsbuild plugins.

    Rstest and Rsbuild share the same plugin system, so you can use Rsbuild plugins in Rstest.

    Using plugins

    You can register Rsbuild plugins in rstest.config.* using the plugins option, see Rsbuild - plugins.

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    import { pluginReact } from '@rsbuild/plugin-react';
    
    export default defineConfig({
      plugins: [pluginReact()],
    });

    Discover plugins

    Check out the Rsbuild plugin list to discover available plugins. These plugins can also be used with Rstest.

    Modify Rstest config in Rsbuild plugins

    When an Rsbuild plugin runs inside Rstest, it can get the Rstest exposed API through api.useExposed from the Rsbuild Plugin API and call modifyRstestConfig to adjust the Rstest config for the current project.

    This API is currently exposed only for Node-mode projects. Browser Mode projects do not receive the rstest exposed API, so api.useExposed('rstest') returns undefined there.

    This API is intended for framework integrations and toolchain plugins. When a plugin already knows the current framework, Rsbuild environment, or project conventions, it can centrally add project config such as test entries, exclude rules, setup files, aliases, defines, or testEnvironment so users do not need to duplicate the same information in their Rstest config.

    rstest-plugin.ts
    import type { RsbuildPlugin } from '@rsbuild/core';
    import type { RstestExposeAPI } from '@rstest/core';
    
    export const myPlugin = (): RsbuildPlugin => ({
      name: 'my-plugin',
      setup(api) {
        if (api.context.callerName !== 'rstest') {
          return;
        }
    
        const rstestApi = api.useExposed<RstestExposeAPI>('rstest');
    
        rstestApi?.modifyRstestConfig((config) => {
          config.include = ['**/*.test.ts'];
        });
      },
    });

    In multi-project mode, Rstest exposes one API for each Rsbuild environment. A plugin registered in one project only modifies that project's Rstest config and does not affect other projects.

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    import { myPlugin } from './rstest-plugin';
    
    export default defineConfig({
      projects: [
        {
          name: 'node',
          plugins: [myPlugin()],
        },
        {
          name: 'browser',
          plugins: [],
        },
      ],
    });

    Type definitions

    RstestExposeAPI is exported from @rstest/core and is the type of the API exposed by Rstest to Rsbuild plugins through api.useExposed('rstest').

    import type { RstestConfig } from '@rstest/core';
    
    export type ModifyRstestConfigCallback = (
      config: RstestConfig,
    ) => RstestConfig | void | Promise<RstestConfig | void>;
    
    export type RstestExposeAPI = {
      modifyRstestConfig: (callback: ModifyRstestConfigCallback) => void;
    };

    RstestConfig is the same user-facing config shape accepted by defineConfig. You can either mutate the received config object directly or return a config fragment that matches the RstestConfig shape. Rstest merges, normalizes, and validates these changes again after the callback finishes. The callback can also be asynchronous.

    Register modifyRstestConfig during the Rsbuild plugin setup phase. Do not register it from later Rsbuild config hooks such as api.modifyRsbuildConfig, because Rstest applies collected callbacks while resolving the Rsbuild config; callbacks registered inside those hooks are too late for the current resolution pass.

    Modifiable config fields

    modifyRstestConfig only adjusts the current project's config. If a callback modifies an unsupported field, Rstest throws an error that tells you to configure it in rstest.config.* instead.

    Config scopeSupportedNotes
    include, exclude, includeSourceYesAffect test discovery for the current project; Rstest recollects test entries after the callback.
    setupFiles, globalSetupYesAffect setup files for the current project; Rstest resolves them again after the callback.
    testEnvironmentYesOnly affects the current project's test environment.
    resolve, source, performance.buildCacheYesReapplied to Rsbuild as build config for the current project.
    root, output.moduleYesRe-normalized as path or output module config for the current project.
    name, browser.enabledNoThey change project identity or runtime mode and must be declared in rstest.config.*.
    projects, plugins, extendsNoThey change project topology or plugin initialization order and must be declared in Rstest config or an adapter.
    coverage, reporters, pool, isolate, update, shard, forceRerunTriggersNoThey are global execution strategy fields or participate in scheduling before Rsbuild plugins run.
    output.distPathNoIt changes the Rstest/Rsbuild output directory topology and must be declared in Rstest config.

    If you need to add Rsbuild plugins dynamically, integrate through Rstest's extends adapter pattern instead. An adapter can prepare the Rstest config before Rsbuild plugin initialization starts.