babel-jest-amcharts
raw JSON → 0.0.2 verified Sat Apr 25 auth: no javascript
A Babel Jest transformer plugin customized to work with amCharts libraries. Version 0.0.2 is the latest and only stable release. It patches babel-jest to properly transform amCharts ESM modules during Jest testing. Designed specifically for amCharts 4+ and React projects. Differentiates from generic babel-jest by handling amCharts' non-standard module exports and preventing transformation errors that occur with default Jest configuration.
Common errors
error Cannot find module 'babel-jest-amcharts' from 'jest.config.js' ↓
cause Missing installation of babel-jest-amcharts or its peer dependencies.
fix
Run 'npm install --save-dev babel-jest-amcharts @babel/core babel-jest babel-preset-react-app'
error SyntaxError: Cannot use import statement outside a module for @amcharts/amcharts4/core ↓
cause Jest is not transforming the amCharts ESM modules because of incorrect transformIgnorePatterns.
fix
Update transformIgnorePatterns to include '[/\\\\]node_modules[/\\\\](?!(@amcharts)\\/).+\\.(js|jsx|ts|tsx)$'
Warnings
gotcha The transformIgnorePatterns must include a negative lookahead for @amcharts to allow transformation of amCharts modules. ↓
fix Set transformIgnorePatterns as shown in the README.
gotcha This package overrides the default babel-jest transformer. If you also use TypeScript, you may need additional configuration. ↓
fix Consider using ts-jest or babel-jest with TypeScript preset alongside this plugin.
Install
npm install babel-jest-amcharts yarn add babel-jest-amcharts pnpm add babel-jest-amcharts Imports
- default wrong
import babelJest from 'babel-jest-amcharts'correctmodule.exports = require('babel-jest-amcharts') - createTransformer wrong
import { createTransformer } from 'babel-jest-amcharts'correctconst { createTransformer } = require('babel-jest-amcharts') - getCacheKey wrong
import { getCacheKey } from 'babel-jest-amcharts'correctconst { getCacheKey } = require('babel-jest-amcharts')
Quickstart
// Install the package and its peer deps
npm install --save-dev babel-jest-amcharts @babel/core babel-jest babel-preset-react-app
// In jest.config.js or package.json:
"transform": {
"^.+\\.(js|jsx)$": "babel-jest-amcharts"
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\](?!(@amcharts)\\/).+\\.(js|jsx|ts|tsx)$"
]
// Example test (__tests__/amcharts.test.js):
import * as am4core from '@amcharts/amcharts4/core';
test('amCharts loads', () => {
expect(am4core).toBeDefined();
});