For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /config/test/federation.md.
close
  • English
  • federation

    • Type: boolean
    • Default: false
    • CLI: --federation

    Whether to enable Module Federation support.

    Rstest supports Module Federation in both Node Mode and Browser Mode. Node-based runners, including testEnvironment: 'node' and DOM-simulated environments such as jsdom and happy-dom, use Rstest's compatibility shims for federation runtimes that load chunks through Node APIs. Node Mode support is available from Rstest 0.11.4. Browser Mode uses the bundler's web federation runtime and requires Rstest 0.11.10 or newer.

    Rstest evaluates test bundles inside a single worker runtime so that module mocks stay effective. Module Federation runtimes load remote entries and chunks on their own — via Node fs/vm/eval or over HTTP — outside of that runtime. When federation is enabled, Rstest installs additional runtime shims to make the two work together:

    • Externalized dynamic imports get a globalThis fallback, so federation runtime chunks evaluated via vm/eval can still load modules with Node's native dynamic import.
    • Module Federation's placeholder chunk handlers (consumes / remotes) are kept from throwing before the federation runtime initializes.
    • Federation runtime plugins cannot replace Rstest's chunk-loading handlers, which would otherwise evaluate chunks outside the test runtime and lose mocks.
    • Node-targeted federation runs use Rstest's CommonJS worker loader; Rstest forces output.chunkLoading: 'require' so async chunks load through Rstest's module loader from the in-memory build output instead of the filesystem.
    CLI
    rstest.config.ts
    npx rstest --federation

    Testing a federated app

    federation only prepares the test runtime; it does not configure remotes, exposes, or shared modules. When you use @module-federation/rstest, the plugin configures the Module Federation build and automatically enables Rstest's compatibility mode for Node-based test environments, so do not also set federation: true.

    Start with the Module Federation guide for installation and the Rstest testing workflow. See the official Rstest integration guide for reusing an Rsbuild federation configuration and the complete plugin options.

    rstest.config.ts
    import { federation } from '@module-federation/rstest';
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      plugins: [
        federation({
          name: 'main_app',
          remotes: {
            'component-app': 'component_app@http://localhost:3001/remoteEntry.cjs',
          },
          shared: {
            react: { singleton: true },
          },
        }),
      ],
    });

    This Node-based example consumes a Node-targeted remoteEntry.cjs. Browser Mode should consume the browser build's remoteEntry.js. See the Module Federation guide for the distinction and the federation example for a complete setup with Browser Mode, HTTP remotes, local CommonJS remotes, and SSR tests.