Registry / testing / babel-plugin-rewire-exports

babel-plugin-rewire-exports

JSON →
library2.3.0jsnpmunverified

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-exports
INSTALL
IMPORT
SIG · BABEL-PLUGIN-REWIR
B
babel-plugin-rewire-exports
testingjavascriptv2.3.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

rewire (default export stub)
✓ import { rewire } from './my-module'
✗ import rewire from './my-module'
The plugin exports rewire as a named export, not default. It replaces the default export of the module.
rewire$name (named export stub)
✓ import { rewire$name } from './my-module'
✗ import { rewire_name } from './my-module'
For named exports, the stub is rewire$<exportName>. The dollar sign is part of the identifier.
restore
✓ import { restore } from './my-module'
✗ import restore from './my-module'
restore is a named export from the transformed module, not default.

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!'
Debug
Known issues
breakingv2.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.
affects: >=2.0.0
deprecatedv1.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.
affects: <1.3.0 >=1.0.0
gotchaPlugin 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"]
affects: >=1.0.0
gotchaDefault 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.
affects: >=1.0.0
gotchaIf 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.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Plugin babel-plugin-rewire-exports requires @babel/core but was not found.
Missing @babel/core peer dependency.
fix
Run npm install --save-dev @babel/core babel-plugin-rewire-exports
TypeError: Cannot assign to read only property 'message' of object '#<Object>'
Attempting to rewire a constant export without unsafeConst option.
fix
Add "unsafeConst": true to plugin options, or change export to let/var.
SyntaxError: Unexpected token $
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.
Upgrade
Version history
2.3.0latest on npm
Audit
Dependencies
@babel/corerequiredRequires Babel 7 core as peer dependency.
Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
1
Resources
babel-plugin-rewire-exports — npm install babel-plugin-rewire-exports · libregistry