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

    output.module output.moduleoutput.module

    • Type: boolean
    • Default: true (false when federation is true)
    • CLI: --output.module

    Controls whether Rstest outputs JavaScript test bundles in ES module format.

    By default, Rstest emits and executes test bundles as ES modules. Setting federation: true forces this option to false. To output CommonJS bundles directly, set output.module to false:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      output: {
        module: false,
      },
    });

    CommonJS interop

    When output.module is true, Rstest infers the external type from the import syntax:

    • Dependencies imported with import are treated as ESM externals.
    • Dependencies imported with require are treated as CommonJS externals.

    When importing a CommonJS module with import syntax, Rstest applies interop so named and default exports work for modules like this:

    cjs-module
    Object.defineProperty(exports, '__esModule', { value: true });
    
    const a = require('./a');
    
    exports.a = a.a;
    
    exports.default = () => {
      return `hello ${a.a}`;
    };
    test/index.test.ts
    import defaultExport, { a } from 'cjs-module';

    However, Rstest cannot interop properties attached to a module.exports function as named ESM imports:

    function lodash(_value) {}
    
    lodash.VERSION = VERSION;
    
    module.exports = lodash;
    import { VERSION } from 'lodash'; // ❌

    In that case, explicitly mark the dependency as CommonJS via specifying external type:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      output: {
        externals: {
          lodash: 'commonjs lodash', // externalize lodash as a CommonJS module
        },
      },
    });

    output.externals output.externalsoutput.externals

    • Type: Rspack.Externals
    • Default: undefined

    Prevents specified dependencies from being bundled so Rstest resolves them at runtime instead.

    • In the node test environment, Rstest bundles TypeScript/JSX files (.ts, .tsx, .jsx, .mts, .cts) and non-node_modules JavaScript files (.js, .mjs, .cjs), and externalizes node_modules dependencies by default.
    • In browser-like environments (jsdom, happy-dom), all packages are bundled by default.

    To externalize a specific dependency, add it to output.externals:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      output: {
        externals: ['react'],
      },
    });

    To bundle all third-party dependencies instead of externalizing them, set output.bundleDependencies to true:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      output: {
        bundleDependencies: true,
      },
    });

    Specify external type

    You can use ${externalsType} ${libraryName} syntax to specify the external type of a dependency.

    For example, you can use commonjs lodash to specify that lodash should be externalized as a CommonJS module:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      output: {
        externals: {
          lodash: 'commonjs lodash',
        },
      },
    });

    You can also specify the default external type for all dependencies through the externalsType configuration option.

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      tools: {
        rspack: {
          externalsType: 'commonjs',
        },
      },
    });

    output.bundleDependencies

    • Type: boolean | (string | RegExp)[]
    • Default: Depends on testEnvironment

    Controls whether third-party dependencies from node_modules are bundled or externalized.

    • true: Always bundle all third-party dependencies, regardless of test environment.
    • false: Always externalize third-party dependencies, regardless of test environment.
    • ['pkg-name']: Bundle only the listed packages and externalize the rest.
    • ['pkg-name/subpath']: Bundle a specific package subpath.
    • ['pkg-name/*']: Bundle package requests that match the glob-like pattern.
    • [/^pkg-name\\/subpath/]: Bundle package requests that match the regular expression.

    When this option is unset, Rstest bundles dependencies in browser-like test environments (jsdom, happy-dom, etc.), and externalizes them in the node environment.

    Warning

    This option only applies to non-browser mode. In browser mode, all dependencies are always bundled, so output.bundleDependencies: false is not supported.

    This option provides a simple way to override the default bundling strategy that is tied to testEnvironment. For example, if you use jsdom but want the same externalization behavior as the node environment:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      testEnvironment: 'jsdom',
      output: {
        bundleDependencies: false,
      },
    });

    Or if you want to bundle all dependencies in the node environment to benefit from optimizations like lazy barrel:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      testEnvironment: 'node',
      output: {
        bundleDependencies: true,
      },
    });

    If you only want to bundle a few packages while keeping the rest externalized, pass their package names:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      output: {
        bundleDependencies: ['strip-ansi'],
      },
    });

    You can also target subpaths or patterns:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      output: {
        bundleDependencies: ['strip-ansi/lib/index.js', 'strip-ansi/*'],
      },
    });

    Patterns only affect dependency requests that rstest can still process in the current bundle graph. If a package has already been externalized, its internal dependencies stay external as well. In other words, bundleDependencies does not re-bundle transitive dependencies that are reachable only through an externalized parent package.

    Relationship with output.externals

    Both options decide whether a dependency is bundled or externalized, but they work at different levels. Think of it as "set the baseline first, then make exceptions":

    • output.bundleDependencies sets the overall baseline: it decides, in one shot, whether all node_modules dependencies default to being bundled or externalized.
    • output.externals makes per-package exceptions: on top of that baseline, it targets individual packages, and takes higher priority.

    Listing a package has the opposite effect

    When you only need to adjust a few packages, the two options express opposite intents:

    • bundleDependencies: ['foo'] externalizes every dependency by default and bundles only foo.
    • externals: ['foo'] bundles every dependency by default and externalizes only foo.

    Which one to use depends on the behavior most of your dependencies should keep:

    • Most dependencies should stay externalized, and only a few need bundling → list the packages to bundle in bundleDependencies.
    • Most dependencies should stay bundled, and only a few need externalizing → list the packages to externalize in output.externals. This assumes a bundling baseline (a browser-like environment, or bundleDependencies: true), since output.externals only adds exceptions on top of the current baseline and does not change it.

    When both are set, output.externals wins

    output.externals is a native Rspack capability and takes effect before Rstest's overall strategy. So you can use bundleDependencies to set a broad baseline, then use externals to precisely override individual packages:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      output: {
        // Baseline: bundle all third-party dependencies
        bundleDependencies: true,
        // Exception: keep lodash externalized
        externals: ['lodash'],
      },
    });

    output.cssModules output.cssModulesoutput.cssModules

    • Type: CSSModules
    • Default: {}

    Customizes CSS Modules behavior (such as auto, localIdentName, namedExport, and exportLocalsConvention) using Rsbuild's CSS Modules options.

    output.emitAssets output.emitAssetsoutput.emitAssets

    • Type: boolean
    • Default: true
    • CLI: --output.emitAssets

    Controls whether imported static assets such as images, fonts, audio, and video are emitted as build assets during test builds.

    When true, imported asset modules are emitted into the build output file system; when false, they are not emitted. Emitted assets are written to disk only when dev.writeToDisk is enabled or when running with DEBUG output enabled; otherwise they remain in the temporary in-memory output.

    If you reuse an Rsbuild config through @rstest/adapter-rsbuild, output.emitAssets is inherited automatically.

    output.distPath output.distPathoutput.distPath

    • Type: string | { root?: string }
    • Default: { root: 'dist/.rstest-temp' }

    Sets the global root directory for Rstest's temporary build outputs.

    By default, Rstest keeps temporary build files in memory. When dev.writeToDisk is enabled, Rstest writes compiled Node.js test bundles and Browser Mode server/runner outputs under dist/.rstest-temp; running with DEBUG also writes .rsbuild inspect configs there.

    In multi-project runs, Rstest uses a single global output root and creates per-project subdirectories under it rather than switching to each project's root.

    To change the temporary output directory, set output.distPath.root:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      dev: {
        writeToDisk: true,
      },
      output: {
        distPath: {
          root: 'custom/.rstest-temp',
        },
      },
    });

    With this config, Rstest uses <root>/custom/.rstest-temp instead of <root>/dist/.rstest-temp.

    output.cleanDistPath output.cleanDistPathoutput.cleanDistPath

    • Type: boolean | 'auto' | { enable?: boolean | 'auto'; keep?: RegExp[] }
    • Default: 'auto'
    • CLI: --output.cleanDistPath

    Controls whether to clean temporary files in the output directory before tests start.

    By default, Rstest does not write temporary test files to disk, so this mainly applies when dev.writeToDisk is enabled.