babel-plugin-tester
raw JSON → 12.0.0 verified Sat Apr 25 auth: no javascript
Utilities for writing tests for Babel plugins and presets. Version 12.0.0 is current. The library supports Jest, Mocha, Jasmine, node:test, and Vitest. Key differentiators: automatic fixture file loading, built-in prettier formatting, snapshot-based testing, environment variable filtering (TEST_ONLY, TEST_SKIP), and async ResultFormatter. Breaking changes in v12 include dropping default exports, requiring Node.js >=20.18.0, and adopting prettier@3's async interface. Peer dependency on @babel/core >=7.22.0.
Common errors
error TypeError: pluginTester is not a function ↓
cause Using default import with v12+ which removed default export.
fix
Change to named import:
import { pluginTester } from 'babel-plugin-tester' error Error: Cannot find module 'babel-plugin-tester' ↓
cause Missing install or wrong import path.
fix
Run
npm install --save-dev babel-plugin-tester and ensure import path is correct. error AssertionError: expected 'const x = 1;' to equal 'const x = 2;' ↓
cause Test output mismatch; plugin may not be transforming as expected.
fix
Check plugin logic and verify test input/output.
Warnings
breaking Default export removed in v12. Import { pluginTester } instead of default import. ↓
fix Change `import pluginTester from 'babel-plugin-tester'` to `import { pluginTester } from 'babel-plugin-tester'`.
breaking Minimum Node.js version is now 20.18.0 (v12). ↓
fix Upgrade Node.js to >=20.18.0.
breaking ResultFormatter is now async (returns Promise) due to prettier@3. ↓
fix If using custom formatter, ensure it returns a Promise or use async/await.
gotcha With prettier@3, some Node versions require --experimental-vm-modules flag. ↓
fix Run tests with `NODE_OPTIONS='--no-warnings --experimental-vm-modules' npx jest`.
breaking Implicit global option merging removed in v11. All options must be explicitly set per test or via global config. ↓
fix Move shared options to the top-level pluginTester configuration.
Install
npm install babel-plugin-tester yarn add babel-plugin-tester pnpm add babel-plugin-tester Imports
- pluginTester wrong
import pluginTester from 'babel-plugin-tester'correctimport { pluginTester } from 'babel-plugin-tester' - prettierFormatter
import { prettierFormatter } from 'babel-plugin-tester' - pluginTester (CJS) wrong
const pluginTester = require('babel-plugin-tester')correctconst { pluginTester } = require('babel-plugin-tester')
Quickstart
import { pluginTester } from 'babel-plugin-tester';
import myPlugin from './my-plugin';
pluginTester({
plugin: myPlugin,
tests: {
'transforms correctly': {
code: 'const x = 1;',
output: 'const x = 1;',
},
'snapshot test': {
code: 'const y = 2;',
snapshot: true,
},
},
});