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.
npm install babel-plugin-rewire-exportsVerified import paths — ran on the pinned version, not inferred.
Shows basic setup and usage: rewire a named export, restore original.
In tests, access the function via the variable export, not the original function declaration. Use rewire$default for default exports.
If relying on rewiring const inside the module, use unsafeConst option or upgrade to >=1.3.0 and export as let/var.
In .babelrc, order: ["babel-plugin-rewire-exports", "@babel/plugin-transform-modules-commonjs"]
Use rewire(stub) for default exports, not named import default.
Check for name collisions and use the renamed stubs accordingly.
Run npm install --save-dev @babel/core babel-plugin-rewire-exports
Add "unsafeConst": true to plugin options, or change export to let/var.
Upgrade Node or use a bundler that supports the plugin's output.