babel-plugin-rewire

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

A Babel plugin that adds __Rewire__, __ResetDependency__, and __GetDependency__ methods to modules for mocking dependencies in tests. Version 1.2.0 is stable with moderate release cadence. It supports ES6 import, CommonJS require(), and top-level variable/function rewiring. Unlike proxyquire or rewire.js, it works at the AST level via Babel, enabling rewiring of named exports and internal dependencies. Supports both default and named exports, and automatically handles primitive vs object default exports. Compatible with React and other transpiled environments.

error __Rewire__ is not a function
cause Module not transformed by babel-plugin-rewire or imported as CJS before transformation.
fix
Ensure the module is processed by Babel with the plugin. For Node tests, use @babel/register or equivalent.
error Cannot find module 'babel-plugin-rewire'
cause Plugin not installed or missing from node_modules.
fix
Run: npm install --save-dev babel-plugin-rewire
error Cannot assign to read only property '__Rewire__' of object '#<Object>'
cause Using strict mode or frozen object; plugin cannot attach methods to immutable default exports.
fix
Use __RewireAPI__ instead or avoid freezing exports.
error Cannot rewire a dependency that is not explicitly imported in module.
cause Plugin can only rewire dependencies that are statically analyzable in the module source (import/require at top level).
fix
Ensure the dependency is imported at module top level.
breaking Peer dependency @babel/core v7+ required; not compatible with Babel 6.
fix Install @babel/core: npm install --save-dev @babel/core
deprecated __set__ and __get__ are deprecated in favor of __Rewire__ and __GetDependency__.
fix Use __Rewire__('dep', value) and __GetDependency__('dep') instead.
gotcha Default exports of primitive types (string, number, boolean) do not receive __Rewire__ methods; use __RewireAPI__ instead.
fix Access via import { __RewireAPI__ } from 'module' or use __RewireAPI__.__Rewire__()
gotcha Rewiring a named export does not affect other modules that import that same named export.
fix Only changes the internal reference within the rewired module.
gotcha Top-level variable rewiring works only for variables initialized with require() or literals, not for complex expressions.
fix Refactor complex initializations into separate functions or use __Rewire__ on the entire module export if possible.
npm install babel-plugin-rewire
yarn add babel-plugin-rewire
pnpm add babel-plugin-rewire

Basic configuration and usage of babel-plugin-rewire: define module, rewire function, call, reset.

// babel.config.js
module.exports = {
  plugins: [['babel-plugin-rewire', { 'rewire': { 'exclude': /node_modules/ } }]]
};

// myModule.js
export function greet(name) {
  return `Hello ${name}`;
}

// test.js
import myModule from './myModule';
myModule.__Rewire__('greet', (name) => `Hi ${name}`);
const result = myModule.greet('World');
console.log(result); // 'Hi World'
myModule.__ResetDependency__('greet');
console.log(myModule.greet('World')); // 'Hello World'