Testing functionality specifically designed for Babel presets with preset-specific configuration options.
Configuration options for testing Babel presets instead of individual plugins.
interface PluginTesterOptions {
/** The babel preset under test */
preset?: (...args: any[]) => Babel.TransformOptions;
/** Name for the preset (used in test descriptions) */
presetName?: string;
/** Options passed to the preset */
presetOptions?: any;
}Usage Examples:
import pluginTester from "babel-plugin-tester";
import myPreset from "../my-preset";
// Basic preset testing
pluginTester({
preset: myPreset,
presetName: "my-typescript-preset",
tests: [
{
code: `
interface User {
name: string;
age: number;
}
const user: User = { name: "John", age: 30 };
`,
output: `
const user = { name: "John", age: 30 };
`,
},
],
});
// Preset with options
pluginTester({
preset: myPreset,
presetName: "react-preset",
presetOptions: {
runtime: "automatic",
development: true,
},
tests: [
{
code: `
function App() {
return <div>Hello World</div>;
}
`,
output: `
import { jsx as _jsx } from "react/jsx-dev-runtime";
function App() {
return _jsx("div", { children: "Hello World" });
}
`,
},
],
});Special symbol for controlling preset execution context.
/** Symbol to run preset in the current test context */
const runPresetUnderTestHere: unique symbol;Usage Example:
import { pluginTester, runPresetUnderTestHere } from "babel-plugin-tester";
pluginTester({
preset: myPreset,
tests: [
{
code: `
import { runPresetUnderTestHere } from "babel-plugin-tester";
runPresetUnderTestHere;
const Component = () => <div>Test</div>;
`,
output: `
const Component = () => React.createElement("div", null, "Test");
`,
},
],
});Important limitation: cannot test both plugins and presets simultaneously.
// ❌ This will throw an error
pluginTester({
plugin: myPlugin,
preset: myPreset, // Error: cannot specify both
tests: [...]
});
// ✅ Test plugin or preset separately
pluginTester({
plugin: myPlugin,
tests: [...]
});
pluginTester({
preset: myPreset,
tests: [...]
});Preset names default to "unknown preset" if not provided and cannot be inferred.
// Explicit preset name
pluginTester({
preset: myPreset,
presetName: "my-custom-preset",
tests: [...]
});
// Will use "unknown preset" as default
pluginTester({
preset: myPreset,
tests: [...]
});