babel-test
raw JSON → 0.2.4 verified Sat Apr 25 auth: no javascript
An opinionated library for testing Babel plugins with Jest or Mocha, using fixture files instead of snapshots. Current stable version is 0.2.4 (last released October 2020, low release cadence). Integrates with Jest's snapshot feature to auto-update fixture outputs on keypress, avoiding manual copy-paste. Simpler API than alternatives like @babel/helper-plugin-test-runner or babel-plugin-tester; focuses on directory-based fixtures with code.js/output.js/error.js pattern. Ships TypeScript types.
Common errors
error Cannot find module 'babel-core' ↓
cause babel-core is a peer dependency and not installed automatically.
fix
Run npm install --save-dev @babel/core (or babel-core for v6).
error ENOENT: no such file or directory, open '.../__fixtures__/test/code.js' ↓
cause Fixture directory structure is incorrect; missing code.js file.
fix
Ensure each fixture subdirectory contains a code.js file. See documentation for required structure.
error TypeError: (0 , _babelTest.create) is not a function ↓
cause Default import used instead of named import.
fix
Change import create from 'babel-test' to import { create } from 'babel-test'.
Warnings
gotcha The 'create' function sets babelrc and configFile to false by default to avoid external config interference. ↓
fix If you need external config, pass babelrc: true or configFile: true explicitly in the config object.
deprecated The 'test' function returned from create() is undocumented and may be removed in future versions. ↓
fix Use 'fixtures' instead for directory-based tests. For single-file tests, consider using 'babel-plugin-tester'.
gotcha Fixture directories prefixed with 'skip.' or 'only.' are case-sensitive and must be lowercase. ↓
fix Name directories exactly as 'skip.my-test' or 'only.my-test' (all lowercase before dot).
gotcha If using Jest's --updateSnapshot flag, fixture output files are overwritten automatically; this may cause data loss if output.js is hand-edited. ↓
fix Version control fixture files. Use --ci mode to prevent accidental updates.
Install
npm install babel-test yarn add babel-test pnpm add babel-test Imports
- create wrong
const create = require('babel-test')correctimport { create } from 'babel-test' - default import wrong
import create from 'babel-test'correctimport { create } from 'babel-test' - test wrong
const { test } = require('babel-test')correctconst { test } = create({ plugins: [...] })
Quickstart
import path from 'path';
import { create } from 'babel-test';
const { fixtures } = create({
plugins: [['path/to/plugin', { option1: true }]],
});
fixtures('my plugin tests', path.join(__dirname, '__fixtures__'), {
beforeEach() {
console.log('Running setup...');
},
});
// Directory structure:
// __fixtures__/
// simple-transform/
// code.js
// output.js
// error-case/
// code.js
// error.js