rollup-plugin-mock-imports
raw JSON → 1.0.8 verified Mon Apr 27 auth: no javascript maintenance
Rollup plugin (v1.0.8) for mocking ESM imports during bundling, using `__mocks__` directories (like Jest) or a custom `node_mockdules` folder. Designed for pre-compilation testing scenarios (e.g., Svelte components with JSDOM), it intercepts resolved module IDs before other Rollup plugins. Configuration options include `mockall`, `ignore`, and `mock` patterns. Requires Rollup ^0.66.6 as peer dependency. Ships TypeScript types. Release cadence: low (last updated 2019).
Common errors
error Error: Cannot find module 'rollup' ↓
cause Rollup is a peer dependency and not installed automatically
fix
npm install --save-dev rollup@^0.66.6
error 'mockImports' is not exported from 'rollup-plugin-mock-imports' ↓
cause Default import used instead of named import
fix
Use import { mockImports } from 'rollup-plugin-mock-imports';
Warnings
breaking Rollup peer dependency is ^0.66.6 – may not work with Rollup >=1.0.0 ↓
fix Use Rollup ^0.66.6 or test compatibility with newer versions; consider forking or updating if needed.
gotcha mockImports must come before other module resolution plugins (e.g., rollup-plugin-node-resolve) or mocks may be skipped ↓
fix Place mockImports() first in the plugins array.
gotcha Mock imports only work for static ES imports; dynamic import() or CommonJS require() are not mocked ↓
fix Ensure imports are static ESM statements; convert dynamic imports to static or use alternative mocking.
gotcha Plugin creates a virtual node_mockdules folder alongside node_modules – name is critical and non-standard ↓
fix Do not rename the folder; the path is hardcoded relative to node_modules. For custom paths, use nodePath option.
Install
npm install rollup-plugin-mock-imports yarn add rollup-plugin-mock-imports pnpm add rollup-plugin-mock-imports Imports
- mockImports wrong
import mockImports from 'rollup-plugin-mock-imports'correctimport { mockImports } from 'rollup-plugin-mock-imports'
Quickstart
// rollup.config.js
import { mockImports } from 'rollup-plugin-mock-imports';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
export default {
input: 'src/main.js',
plugins: [
mockImports({ mockall: true, ignore: ['fs', 'path'] }),
resolve(),
commonjs()
],
output: { file: 'dist/bundle.js', format: 'esm' }
};