babel-plugin-mock-imports
raw JSON → 1.2.0 verified Sat Apr 25 auth: no javascript
Babel plugin for redirecting import statements to different locations. Current version 1.2.0, first released as 0.0.1. Key differentiator: simple configuration via RegExp patterns, useful for mocking modules or aliasing imports. Stable API with minimal changes.
Common errors
error Error: .babelrc plugin is not a string or object/fn ↓
cause Plugins array item is incorrectly formatted.
fix
Use an array: ['mock-imports', { redirects: [...] }]
error TypeError: Cannot read property 'redirects' of undefined ↓
cause Plugin options object is missing or incorrect.
fix
Ensure options object has 'redirects' array.
Warnings
gotcha Pattern strings are evaluated as RegExp; special characters must be escaped correctly. ↓
fix Escape regex special characters in pattern strings, or use strings that are safe for RegExp.
gotcha Plugin replaces import source strings but does not transform default/namespace imports; ensure mock modules export the same interface. ↓
fix Ensure mock modules export the same symbols as the original modules.
Install
npm install babel-plugin-mock-imports yarn add babel-plugin-mock-imports pnpm add babel-plugin-mock-imports Imports
- plugin wrong
require('babel-plugin-mock-imports')correct['mock-imports', { redirects: [...] }] in .babelrc or babel.config.js - redirects wrong
pattern: /.svg$/correctpattern: '.(svg)$', location: 'path/to/mock' - type definition
No official type definitions; use @types/babel__core for general Babel types.
Quickstart
module.exports = {
plugins: [
['mock-imports', {
redirects: [
{
pattern: '.svg$',
location: 'path/to/mocked/react/component'
}
]
}]
]
};