{"id":19053,"library":"babel-plugin-explicit-exports-references","title":"Babel Plugin Explicit Exports References","description":"A Babel plugin that transforms internal references to a module's exports so they use `module.exports` instead of direct local variable references. This enables mocking of exported functions in Jest with Babel/TypeScript, even when those functions call each other internally within the same module. Version 1.0.2, actively maintained. Key differentiator: addresses the common problem of internal function references not being mockable in test environments, unlike other solutions that require manual refactoring or alternative mocking strategies.","status":"active","version":"1.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/Xunnamius/babel-plugin-explicit-exports-references","tags":["javascript","babel","rewire","jest","function","mock","spy","exported","single","typescript"],"install":[{"cmd":"npm install babel-plugin-explicit-exports-references","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-explicit-exports-references","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-explicit-exports-references","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as a peer dependency to function as a Babel plugin","package":"@babel/core","optional":true}],"imports":[{"note":"This is a Babel plugin, not a regular import. It is configured in babel.config.js as a string reference.","wrong":"import plugin from 'babel-plugin-explicit-exports-references'","symbol":"default","correct":"module.exports = { plugins: ['explicit-exports-references'] }"},{"note":"Options are passed as an array with the plugin name, not as an object directly.","wrong":"plugins: [['explicit-exports-references', { transformAssignExpr: 'true' }]]","symbol":"plugin options","correct":"plugins: [['explicit-exports-references', { transformAssignExpr: true }]]"},{"note":"Use Babel's env property to conditionally enable the plugin only in test environments.","wrong":"module.exports = { plugins: ['explicit-exports-references'], only: ['test'] }","symbol":"env-specific configuration","correct":"module.exports = { env: { test: { plugins: ['explicit-exports-references'] } } }"}],"quickstart":{"code":"// install: npm install --save-dev babel-plugin-explicit-exports-references\n// babel.config.js\nmodule.exports = {\n  presets: ['@babel/preset-env'],\n  env: {\n    test: {\n      plugins: ['explicit-exports-references']\n    }\n  }\n};\n\n// Example: myModule.ts\nexport function foo() {\n  throw new Error('expensive');\n}\nexport function bar() {\n  foo();\n  return 5;\n}\n\n// After transformation (only in test), bar internally calls module.exports.foo() instead of foo()\n// So mocking module.exports.foo in Jest works:\n// myModule.test.ts\nimport * as myModule from './myModule';\njest.spyOn(myModule, 'foo').mockImplementation(() => {});\nconst result = myModule.bar();\nexpect(myModule.foo).toHaveBeenCalled();\nexpect(result).toBe(5);","lang":"typescript","description":"Installs and configures the plugin to enable mocking of internal function references in Jest tests."},"warnings":[{"fix":"Only enable the plugin when NODE_ENV is 'test' using Babel's env configuration.","message":"Using this plugin in production or non-test environments can increase build size and introduce performance overhead.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Do not rely on this plugin for mocking enum references; consider using modules or classes instead.","message":"TypeScript enums are explicitly ignored and will not be transformed to use module.exports.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Avoid using { transformAssignExpr: true } unless absolutely necessary, and test thoroughly.","message":"The 'transformAssignExpr' option is considered unstable and may cause unexpected behavior.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"If you need assignment expressions transformed, enable the experimental 'transformAssignExpr' option.","message":"Assignment expressions are not transformed by default; only identifier references are.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"No fix needed; this is intended behavior as cross-module mocking is handled by Jest's module system.","message":"The plugin only transforms identifier references within the same module; references from other modules are unaffected.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run 'npm install --save-dev babel-plugin-explicit-exports-references' to install the plugin.","cause":"The plugin is not installed as a dev dependency or is not in node_modules.","error":"Cannot find module 'babel-plugin-explicit-exports-references'"},{"fix":"Ensure the transformed code is processed by a bundler like Webpack that provides CommonJS compatibility, or only use in test environments with Jest.","cause":"The plugin transforms code to use `module.exports`, but the environment does not have a CommonJS 'module' object (e.g., running in a browser without bundling).","error":"ReferenceError: module is not defined"},{"fix":"Ensure 'explicit-exports-references' is in your babel.config.js under the 'test' environment and that Jest is using Babel for transformation.","cause":"The Babel plugin is not enabled or configured correctly, so internal references remain as direct function calls.","error":"Jest spyOn does not mock internal calls"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}