{"id":19159,"library":"babel-plugin-rewire-ts","title":"babel-plugin-rewire-ts","description":"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.","status":"maintenance","version":"1.4.0","language":"javascript","source_language":"en","source_url":"git://github.com/rosswarren/babel-plugin-rewire-ts","tags":["javascript","babel","plugin","rewire","es6","modules"],"install":[{"cmd":"npm install babel-plugin-rewire-ts","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-rewire-ts","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-rewire-ts","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This is a babel plugin, not a runtime import. The rewire methods are injected into your modules at build time.","wrong":"const __Rewire__ = require('babel-plugin-rewire-ts').__Rewire__","symbol":"__Rewire__","correct":"import { __Rewire__ } from 'babel-plugin-rewire-ts'"},{"note":"Not directly imported from the plugin; it's exported from the transformed module.","wrong":"","symbol":"__get__","correct":"import ModuleUnderTest from './module';\nModuleUnderTest.__get__('dependency')"},{"note":"Same as above; not a runtime import.","wrong":"","symbol":"__ResetDependency__","correct":"import { __ResetDependency__ } from 'babel-plugin-rewire-ts'"}],"quickstart":{"code":"// Install: npm install babel-plugin-rewire-ts --save-dev\n// .babelrc or babel.config.js\n{\n  \"plugins\": [\n    [\"babel-plugin-rewire-ts\", {}]\n  ]\n}\n\n// module.js\nimport { getData } from './api';\nexport function fetchAndProcess() {\n  return getData().then(data => data.map(x => x * 2));\n}\n\n// test.js\nimport { fetchAndProcess } from './module';\nconst apiMock = { getData: () => Promise.resolve([1, 2, 3]) };\nfetchAndProcess.__Rewire__('getData', apiMock.getData);\nfetchAndProcess().__then(result => {\n  console.log(result); // [2, 4, 6]\n  fetchAndProcess.__ResetDependency__('getData');\n});","lang":"javascript","description":"Demonstrates basic setup and usage of babel-plugin-rewire-ts to mock a module dependency in tests."},"warnings":[{"fix":"Ensure that you reset dependencies after each test to avoid cross-test contamination.","message":"Rewired dependencies do not affect other modules that import the same dependency; each module's rewiring is isolated.","severity":"gotcha","affected_versions":"*"},{"fix":"Update test code to use __Rewire__ and __ResetDependency__ for consistency and proper external state handling.","message":"As of v1.4.0, __Rewire__ and __ResetDependency__ should be used instead of __set__ and __get__ for external state reflection.","severity":"deprecated","affected_versions":">=1.4.0"},{"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.","message":"v1.4.0 syncs internal and external state, which may break tests that relied on the old behavior where rewiring only affected internal state.","severity":"breaking","affected_versions":">=1.4.0"},{"fix":"Ensure that require() calls to be rewired are assigned to top-level variables in the module.","message":"CommonJS require() rewiring works only for top-level var declarations, not arbitrary require calls inside functions.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure you have @babel/preset-typescript installed and configured in your Babel config.","cause":"The plugin may not handle TypeScript syntax properly without appropriate Babel presets.","error":"SyntaxError: Unexpected token (5:10) when using TypeScript with babel-plugin-rewire-ts"},{"fix":"Run npm install babel-plugin-rewire-ts --save-dev and check that it's listed in your Babel plugins.","cause":"The plugin is not installed or is not in the correct location.","error":"Cannot find module 'babel-plugin-rewire-ts' from '...'"},{"fix":"Verify that babel-plugin-rewire-ts is included in your Babel config and that your test files are included in the Babel transformation.","cause":"The module was not transformed by the Babel plugin, likely because the plugin is not configured correctly.","error":"TypeError: ...__Rewire__ is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}