Registry / testing / babel-plugin-rewire-ts

babel-plugin-rewire-ts

JSON →
library1.4.0jsnpmunverified

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.

npm install babel-plugin-rewire-ts
INSTALL
IMPORT
SIG · BABEL-PLUGIN-REWIR
B
babel-plugin-rewire-ts
testingjavascriptv1.4.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

__Rewire__
import { __Rewire__ } from 'babel-plugin-rewire-ts'
const __Rewire__ = require('babel-plugin-rewire-ts').__Rewire__
This is a babel plugin, not a runtime import. The rewire methods are injected into your modules at build time.
__get__
import ModuleUnderTest from './module'; ModuleUnderTest.__get__('dependency')
Not directly imported from the plugin; it's exported from the transformed module.
__ResetDependency__
import { __ResetDependency__ } from 'babel-plugin-rewire-ts'
Same as above; not a runtime import.

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'); });
Debug
Known issues
gotchaRewired 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.
affects: *
deprecatedAs 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.
affects: >=1.4.0
breakingv1.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.
affects: >=1.4.0
gotchaCommonJS 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.
affects: *
Errors
Common errors & fixes
SyntaxError: Unexpected token (5:10) when using TypeScript with babel-plugin-rewire-ts
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.
Cannot find module 'babel-plugin-rewire-ts' from '...'
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.
TypeError: ...__Rewire__ is not a function
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.
Upgrade
Version history
1.4.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
babel-plugin-rewire-ts — npm install babel-plugin-rewire-ts · libregistry