rollup-jest
raw JSON → 3.1.0 verified Mon Apr 27 auth: no javascript
Rollup preprocessor for Jest that transforms files using Rollup. Current version 3.1.0, actively maintained. Allows using Rollup plugins and configuration in Jest tests, supporting ES modules. Key differentiator: integrates Rollup's bundling and plugin ecosystem into Jest's transform pipeline, enabling features like plugin injection, caching, and custom resolve behavior. Peer dependency on Rollup ^2.3.0 || ^3. Provides preset for zero-config setup. Suitable for projects already using Rollup who want to reuse rollup config in tests.
Common errors
error Cannot find module 'rollup-jest' ↓
cause rollup-jest not installed or not in devDependencies
fix
npm install rollup-jest rollup --save-dev
error Cannot use import statement outside a module ↓
cause Jest not configured to transform the file with rollup-jest
fix
Add rollup-jest to Jest transform in jest.config.json
error Jest encountered an unexpected token - SyntaxError: Unexpected token 'export' ↓
cause File not transformed because transform pattern doesn't match
fix
Ensure the file extension matches the regex in Jest config (e.g., "\\.m?js$")
Warnings
breaking Version 3 requires Rollup ^2.3.0 or ^3. Version 2 required Rollup ^1.0.0. ↓
fix Upgrade Rollup to ^2.3.0 or ^3 if using rollup-jest v3.
deprecated The 'useCache' option may cause issues with plugins that have state (e.g. TypeScript). ↓
fix Set 'useCache: false' if experiencing stale results.
gotcha To use ESM configFile with Rollup v3, you must add --experimental-vm-modules to Node.js flags. ↓
fix Run Jest with NODE_OPTIONS=--experimental-vm-modules npm run test
gotcha Default transform only matches .mjs and .js files. Other extensions like .ts or .jsx are not transformed unless explicitly added. ↓
fix Add a custom transform pattern for other extensions, e.g. "\\.ts$": "rollup-jest"
Install
npm install rollup-jest yarn add rollup-jest pnpm add rollup-jest Imports
- default (preset) wrong
Using require('rollup-jest') in jest configcorrect// In jest.config.json: "preset": "rollup-jest" - transform entry wrong
Trying to import rollup-jest as a module in test filescorrect// In jest.config.json: "transform": { "\\.m?js$": "rollup-jest" } - with options wrong
Passing options as second argument to import statementcorrect// In jest.config.json: "transform": { "\\.js$": ["rollup-jest", {"configFile": "./rollup.config.js"}] }
Quickstart
npm install rollup-jest rollup --save-dev
# jest.config.json
{
"jest": {
"preset": "rollup-jest"
}
}
# __tests__/example.test.js
import path from 'path';
test('parses extname', () => {
expect(path.extname('foo.md')).toBe('.md');
});