close

output

output.module output.moduleoutput.module

  • Type: boolean
  • Default: true

Whether to output JavaScript files in ES module format.

Rstest outputs and executes test code in ES module format by default. If you want to output test code in CommonJS format, you can enable it through the following configuration:

rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  output: {
    module: false,
  },
});

Commonjs interop

When you output JavaScript files in ES module format (output.module: true), Rstest will determine the type of external based on how the dependency is imported:

  • Dependencies imported via import syntax will be treated as ES module type externals.
  • Dependencies imported via require syntax will be treated as CommonJS externals.

When you import CommonJS modules using the import syntax, Rstest will attempt interop handling, allowing you to import CommonJS module exports normally using import syntax. The following code works correctly in rstest:

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, this interop handling does not always work perfectly, depending on the way the CommonJS module exports its content. Currently, Rstest does not support interop CommonJS module default exports as named exports.

function lodash(_value) {}

lodash.VERSION = VERSION;

module.exports = lodash;
import { VERSION } from 'lodash'; // ❌

If you encounter issues during usage, you can specify the external type of a dependency as CommonJS through 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

Prevent some import dependencies from being packed into bundles in your code, and instead Rstest will import them at runtime.

  • In the Node.js test environment, Rstest will bundle and transpile the following files by default:
    • Any TypeScript and JSX files in any directory, with file extensions .ts, .tsx, .jsx, .mts, .cts.
    • JavaScript files outside the node_modules directory, with file extensions .js, .mjs, .cjs.
  • In the browser-like (jsdom, etc) test environment, all packages are bundled by default.

If you want a dependency to be externalized, you can configure it in output.externals.

rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  output: {
    externals: ['react'],
  },
});

If you want all dependencies to be bundled, you can configure it through the following configuration:

rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  tools: {
    rspack: (config) => {
      config.externals = [];
    },
  },
});

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.cssModules output.cssModulesoutput.cssModules

For custom CSS Modules configuration.

output.cleanDistPath output.cleanDistPathoutput.cleanDistPath

Whether to clean up all test temporary files under the output directory before the test starts.

By default, rstest does not write test temporary files to disk, and this configuration item may be required when you debug rstest outputs.