Utilities for testing babel plugins
npx @tessl/cli install tessl/npm-babel-plugin-tester@12.0.0babel-plugin-tester provides comprehensive utilities for testing Babel plugins and presets, designed to work seamlessly with popular testing frameworks like Jest, Mocha, Jasmine, node:test, and Vitest. It offers a simple abstraction layer that streamlines the process of writing and executing tests for Babel transformations.
npm install --save-dev babel-plugin-testerESM (default import with automatic Jest setup):
import pluginTester from "babel-plugin-tester";For more control without automatic Jest configuration:
import { pluginTester } from "babel-plugin-tester/pure";CommonJS:
const pluginTester = require("babel-plugin-tester");Named imports (with automatic Jest setup):
import { pluginTester as defaultPluginTester } from "babel-plugin-tester";Additional utilities:
import {
pluginTester,
prettierFormatter,
unstringSnapshotSerializer,
runPluginUnderTestHere,
runPresetUnderTestHere
} from "babel-plugin-tester";import pluginTester from "babel-plugin-tester";
import myBabelPlugin from "../my-babel-plugin";
pluginTester({
plugin: myBabelPlugin,
tests: [
{
code: `const hello = "world";`,
output: `const hello = "WORLD";`,
},
{
code: `
function greet(name) {
return "Hello " + name;
}
`,
output: `
function greet(name) {
return "HELLO " + name;
}
`,
},
],
});babel-plugin-tester is built around several key components:
pluginTester function that orchestrates test executionprettierFormatter for output normalizationCore functionality for testing Babel plugins with various test configurations and execution modes.
function pluginTester(options?: PluginTesterOptions): void;Testing functionality specifically designed for Babel presets with preset-specific configuration options.
interface PluginTesterOptions {
preset?: (...args: any[]) => Babel.TransformOptions;
presetName?: string;
presetOptions?: any;
}Flexible test configuration supporting arrays, objects, and fixture-based testing approaches.
interface TestObject {
code?: string;
output?: string;
fixture?: string;
outputFixture?: string;
/** @deprecated Use fixture instead */
codeFixture?: string;
throws?: ErrorExpectation;
only?: boolean;
skip?: boolean;
setup?: SetupFunction;
teardown?: TeardownFunction;
outputRaw?: OutputTesterFunction;
babelOptions?: Babel.TransformOptions;
pluginOptions?: any;
presetOptions?: any;
filename?: string;
snapshot?: boolean;
formatResult?: ResultFormatter;
}
type ErrorExpectation = boolean | string | RegExp | Error | Class<Error> | ((error: unknown) => boolean);Pluggable result formatters for normalizing and prettifying Babel transformation output.
type ResultFormatter<T = any> = (
code: string,
options?: {
cwd?: string;
filename?: string;
filepath?: string;
} & T
) => Promise<string>;
const prettierFormatter: ResultFormatter<{
prettierOptions?: PrettierOptions | null;
config?: PrettierOptions | null;
}>;Helper functions and symbols for advanced testing scenarios and framework integration.
function restartTestTitleNumbering(): void;
const runPluginUnderTestHere: unique symbol;
const runPresetUnderTestHere: unique symbol;
const unstringSnapshotSerializer: SnapshotSerializer;
const validEndOfLineValues: readonly string[];
const validTitleNumberingValues: readonly (string | boolean)[];interface PluginTesterOptions {
plugin?: (...args: any[]) => Babel.PluginObj<any>;
pluginName?: string;
pluginOptions?: Babel.PluginOptions;
preset?: (...args: any[]) => Babel.TransformOptions;
presetName?: string;
presetOptions?: any;
babel?: {
transform: BabelType['transform'];
transformAsync?: BabelType['transformAsync'];
};
babelOptions?: Omit<Babel.TransformOptions, 'plugins' | 'presets'> & {
plugins?: (NonNullable<Babel.TransformOptions['plugins']>[number] | typeof runPluginUnderTestHere)[] | null;
presets?: (NonNullable<Babel.TransformOptions['presets']>[number] | typeof runPresetUnderTestHere)[] | null;
};
title?: string | false;
filepath?: string;
/** @deprecated Use filepath instead */
filename?: string;
endOfLine?: 'lf' | 'crlf' | 'auto' | 'preserve' | false;
setup?: SetupFunction;
teardown?: TeardownFunction;
formatResult?: ResultFormatter;
snapshot?: boolean;
fixtureOutputName?: string;
fixtureOutputExt?: string;
titleNumbering?: 'all' | 'tests-only' | 'fixtures-only' | false;
restartTitleNumbering?: boolean;
fixtures?: string;
tests?: (TestObject | string)[] | Record<string, TestObject | string>;
}
interface FixtureOptions {
babelOptions?: Babel.TransformOptions;
pluginOptions?: any;
presetOptions?: any;
output?: string;
throws?: ErrorExpectation;
only?: boolean;
skip?: boolean;
setup?: SetupFunction;
teardown?: TeardownFunction;
outputRaw?: OutputTesterFunction;
fixtureOutputName?: string;
fixtureOutputExt?: string;
formatResult?: ResultFormatter;
snapshot?: boolean;
}
type Promisable<T> = T | Promise<T>;
type BabelType = typeof import('@babel/core');
type SetupFunction = () => Promisable<void> | Promisable<TeardownFunction>;
type TeardownFunction = () => Promisable<void>;
type OutputTesterFunction = (output: Babel.BabelFileResult) => Promisable<void>;
type ResultFormatter<T = any> = (
code: string,
options?: {
cwd?: string;
filepath?: string;
/** @deprecated Use filepath instead */
filename?: string;
} & T
) => Promise<string> | string;
type SnapshotSerializer = import('pretty-format').Plugin;