babel-plugin-rewire-exports

raw JSON →
2.3.0 verified Sat Apr 25 auth: no javascript

Babel plugin for stubbing ES6/ES2015 module exports in tests. Version 2.3.0 (stable, last release 2021) allows rewriting exported values in importing modules without modifying module internals. Unlike babel-plugin-rewire, it keeps imports and local variables intact. It generates rewire/stub functions (rewire for default, rewire$name for named exports, and restore). Works with Babel 7 (use 0.x for Babel 6), supports browser and Node ESM, and bundles with Webpack, Rollup, SystemJS, and CommonJS via env preset. Release cadence: infrequent, no recent updates.

error Error: Plugin babel-plugin-rewire-exports requires @babel/core but was not found.
cause Missing @babel/core peer dependency.
fix
Run npm install --save-dev @babel/core babel-plugin-rewire-exports
error TypeError: Cannot assign to read only property 'message' of object '#<Object>'
cause Attempting to rewire a constant export without unsafeConst option.
fix
Add "unsafeConst": true to plugin options, or change export to let/var.
error SyntaxError: Unexpected token $
cause Using node <8.5.0 or browser without ESM support for rewire$name identifier.
fix
Upgrade Node or use a bundler that supports the plugin's output.
breaking v2.0.0: Functions are rewritten as variables. Export default function foo() {} becomes var foo = function foo() {}; export { foo as default };
fix In tests, access the function via the variable export, not the original function declaration. Use rewire$default for default exports.
deprecated v1.3.0: Constant exports behavior changed. Constants can't be rewired within the module; only the exported binding is mutable.
fix If relying on rewiring const inside the module, use unsafeConst option or upgrade to >=1.3.0 and export as let/var.
gotcha Plugin must be placed before @babel/plugin-transform-modules-commonjs when targeting CommonJS.
fix In .babelrc, order: ["babel-plugin-rewire-exports", "@babel/plugin-transform-modules-commonjs"]
gotcha Default export rewiring: rewire() stub replaces the default export, but the original default is kept and can be restored via restore().
fix Use rewire(stub) for default exports, not named import default.
gotcha If the module already has top-level identifiers named rewire or restore, the generated stubs are renamed to rewire$default and restore$rewire.
fix Check for name collisions and use the renamed stubs accordingly.
npm install babel-plugin-rewire-exports
yarn add babel-plugin-rewire-exports
pnpm add babel-plugin-rewire-exports

Shows basic setup and usage: rewire a named export, restore original.

// .babelrc
{
  "plugins": ["babel-plugin-rewire-exports"]
}

// text.js
export let message = 'Hello world!';

// logger.js
import {message} from './text.js';
export default function log() {
  console.log(message);
}

// test.js
import {rewire$message, restore} from './text.js';
import log from './logger.js';

log(); // 'Hello world!'
rewire$message('stubbed');
log(); // 'stubbed'
restore();
log(); // 'Hello world!'