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/guide/debug/troubleshooting.md.
close
  • English
  • Troubleshooting

    This page explains common problems that come from Rstest's execution model. They are often not test failures caused by your assertions, but differences between Rstest, Jest, Vitest, and native Node.js runtime behavior.

    Initialization or mock registration does not run

    If a polyfill, plugin registration, or mock registration in a separate file does not run, check whether the project or dependency package marks the imported module or an intermediate importer as side-effect-free through sideEffects: false, a sideEffects array, or module rules. See Module side effects for declarations and import-chain examples.

    Code outside the bundle graph uses native Node.js behavior

    Rstest runs tests with a bundle-based execution model. Files that are part of the bundle graph are transformed by Rstest's build pipeline, so TypeScript, JSX, aliases, and selected dependency transforms can work before the test worker executes them.

    Some code is still loaded by Node.js at runtime instead of by the bundler. Common examples include:

    • late dynamic require() or import(dynamicPath) calls,
    • modules externalized from the bundle,
    • code loaded by custom Node.js loaders or register hooks,
    • code loaded through Node.js APIs after the test bundle has started.

    For these parts, Rstest currently preserves Node.js native semantics. This makes Node.js flags, --require, --import, and node:module register hooks usable in test workers, but it also means Rstest does not emulate every transform-time convenience from Jest, ts-jest, or Vitest.

    A useful rule of thumb is:

    • Static imports and modules in the bundle graph → let Rstest transform or bundle them.
    • Late dynamic require() or import(dynamicPath) / Node.js loader hooks / externalized modules → expect Node.js native behavior unless you register a loader or change the bundling strategy.

    Runtime require() of CommonJS-style TypeScript files fails in type: module

    Symptom

    A package is configured as ESM with "type": "module", but some runtime-loaded .ts files still contain CommonJS code:

    package.json
    {
      "type": "module"
    }
    plugin.ts
    module.exports = {
      name: 'plugin',
    };
    loader.ts
    import { createRequire } from 'node:module';
    
    const require = createRequire(import.meta.url);
    const plugin = require('./plugin.ts');

    On Node.js versions that support running .ts files directly, require('./plugin.ts') follows Node.js native module classification. In a type: module package scope, Node.js treats plugin.ts as ESM rather than CommonJS, so CommonJS assignments such as module.exports = ... are not exported as a CommonJS value.

    Cause

    Because the dynamically require()d .ts file is loaded by Node.js outside the bundle graph, Node.js strips TypeScript syntax under its native module rules—treating .ts inside a type: module scope as ESM rather than rewriting CommonJS globals (module.exports, require), unlike ts-jest or Vitest runtime transforms.

    Solution

    Prefer making the runtime-loaded file match Node.js native module semantics:

    • Rename the CommonJS TypeScript file to .cts when it must stay CommonJS.
    • Or convert the file to ESM syntax when it lives under type: module.
    • Or move it into a package scope with "type": "commonjs".

    If the dynamic load depends on a custom transform, register that transform explicitly. For migrated Jest projects, the simplest place is setupFiles:

    Install the runtime loader you plan to register first. Rstest does not install these loaders for you; for example, @swc-node/register provides a SWC-based TypeScript require hook, while ts-node provides the ts-node/register hook:

    npm add -D @swc-node/register
    # or: npm add -D ts-node
    test/rstest.setup.ts
    import '@swc-node/register';
    // or: import 'ts-node/register';
    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      setupFiles: ['./test/rstest.setup.ts'],
    });

    If the hook must be active before worker runtime code starts, pass Node.js register flags through pool.execArgv:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      pool: {
        execArgv: ['--require', 'ts-node/register'],
        // or: execArgv: ['--import', './register.mjs'],
      },
    });

    Named imports from CommonJS dependencies fail

    Symptom

    A test or source file imports named exports from a CommonJS dependency:

    import { create } from 'enhanced-resolve';

    When the dependency is executed by Node.js as native ESM, the import may fail because Node.js can only expose named exports that its CommonJS lexer can detect statically.

    Cause

    When a CommonJS dependency runs in Node.js outside the bundle graph, native ESM interop only exposes named exports that Node's CommonJS lexer can statically detect. Packages that define exports dynamically (via getters or Object.defineProperty) fail with import { create } from 'pkg', even though import pkg from 'pkg' works (whereas Vitest may transform more dependencies to hide this).

    Solution

    Prefer the import shape that matches Node.js native CommonJS interop:

    import enhancedResolve from 'enhanced-resolve';
    
    const { create } = enhancedResolve;

    If you need bundler interop for a specific dependency, bundle that dependency so Rstest's build pipeline can process it instead of leaving it to Node.js. In the node environment, third-party dependencies are externalized by default; use output.bundleDependencies to bundle selected packages that need transformation.