A Babel plugin that transforms JavaScript/TypeScript modules to enable mocking of ES imports in tests. Current stable version is 2.0.1. It works with any test runner, bundler, and environment (Node or browser). Unlike tools like proxyquire or rewire, it operates at the Babel AST level, making it compatible with modern ES module syntax. The plugin detects incorrect usage (e.g., mocking non-existent imports) and is designed to minimize runtime overhead. It supports both JavaScript and TypeScript, and can be selectively enabled via Babel's env configuration.
npm install babel-plugin-mockable-importsVerified import paths — ran on the pinned version, not inferred.
Shows installation, Babel config, usage of $imports.$mock and $imports.$restore in a test.
Switch to Node >=12.17 or use ESM-compatible tooling. For backward compatibility, stick with v1.x.
Use Babel's env configuration or conditional builds to only include the plugin for test/development environments.
Ensure the plugin is applied to all modules you want to mock. Use include/exclude options in Babel config.
If you use excludeImportsFromModules, provide a RegExp object instead of a string.
Double-check that the export names you pass to $mock match exactly what the module exports.
Verify the plugin is enabled in your Babel config and that the module is being processed. Check if the module is excluded via exclude or include patterns.
Run `npm install --save-dev babel-plugin-mockable-imports` and ensure your Babel config file is in the project root.
Ensure @babel/core is installed: `npm install --save-dev @babel/core`. Check that the version matches the plugin requirements.
Import $imports from the specific module under test, not from a shared module. Check that the module is transformed.