babel-plugin-rewire-ts

raw JSON →
1.4.0 verified Sat Apr 25 auth: no javascript maintenance

A Babel plugin that enables mocking of module dependencies for testing, based on the rewire.js concept. The current stable version is 1.4.0, with a release cadence that is maintenance-focused. It is a fork of babel-plugin-rewire with added TypeScript compatibility. Key differentiators: it works with TypeScript, supports ES6 imports, CommonJS require(), and named/top-level function rewiring. It provides a RewireAPI object for convenient mocking in tests. It is specifically designed for unit testing where module dependencies need to be replaced with mocks or stubs.

error SyntaxError: Unexpected token (5:10) when using TypeScript with babel-plugin-rewire-ts
cause The plugin may not handle TypeScript syntax properly without appropriate Babel presets.
fix
Ensure you have @babel/preset-typescript installed and configured in your Babel config.
error Cannot find module 'babel-plugin-rewire-ts' from '...'
cause The plugin is not installed or is not in the correct location.
fix
Run npm install babel-plugin-rewire-ts --save-dev and check that it's listed in your Babel plugins.
error TypeError: ...__Rewire__ is not a function
cause The module was not transformed by the Babel plugin, likely because the plugin is not configured correctly.
fix
Verify that babel-plugin-rewire-ts is included in your Babel config and that your test files are included in the Babel transformation.
gotcha Rewired dependencies do not affect other modules that import the same dependency; each module's rewiring is isolated.
fix Ensure that you reset dependencies after each test to avoid cross-test contamination.
deprecated As of v1.4.0, __Rewire__ and __ResetDependency__ should be used instead of __set__ and __get__ for external state reflection.
fix Update test code to use __Rewire__ and __ResetDependency__ for consistency and proper external state handling.
breaking v1.4.0 syncs internal and external state, which may break tests that relied on the old behavior where rewiring only affected internal state.
fix Review tests that use __get__ to fetch internal state; after v1.4.0, the external state should reflect rewires, so __get__ might not be necessary.
gotcha CommonJS require() rewiring works only for top-level var declarations, not arbitrary require calls inside functions.
fix Ensure that require() calls to be rewired are assigned to top-level variables in the module.
npm install babel-plugin-rewire-ts
yarn add babel-plugin-rewire-ts
pnpm add babel-plugin-rewire-ts

Demonstrates basic setup and usage of babel-plugin-rewire-ts to mock a module dependency in tests.

// Install: npm install babel-plugin-rewire-ts --save-dev
// .babelrc or babel.config.js
{
  "plugins": [
    ["babel-plugin-rewire-ts", {}]
  ]
}

// module.js
import { getData } from './api';
export function fetchAndProcess() {
  return getData().then(data => data.map(x => x * 2));
}

// test.js
import { fetchAndProcess } from './module';
const apiMock = { getData: () => Promise.resolve([1, 2, 3]) };
fetchAndProcess.__Rewire__('getData', apiMock.getData);
fetchAndProcess().__then(result => {
  console.log(result); // [2, 4, 6]
  fetchAndProcess.__ResetDependency__('getData');
});