jest-serializer-babel-ast
raw JSON → 0.0.5 verified Sat Apr 25 auth: no javascript maintenance
A Jest snapshot serializer that pretty-prints Babel AST nodes (e.g., from @babel/parser) in a human-readable, indented format. Current stable version is 0.0.5 (unreleased beyond). No active release cadence; maintained sporadically. Differentiators: provides a clean, nested view of AST node properties, ideal for snapshot testing Babel plugins or AST transformations. Compared to alternatives like jest-serialize, it is purpose-built for Babel AST and outputs structured property lists. Works with Jest snapshotSerializers configuration.
Common errors
error Cannot find module 'jest-serializer-babel-ast' ↓
cause Package not installed or not present in node_modules.
fix
Run: npm install jest-serializer-babel-ast --save-dev
error TypeError: serializer.serialize is not a function ↓
cause Importing the package incorrectly, e.g., using import default or expecting named exports.
fix
Use: const serializer = require('jest-serializer-babel-ast');
Warnings
gotcha Works only with Babel AST nodes from @babel/parser (babylon). May not handle other parsers or custom AST types. ↓
fix Ensure you are using @babel/parser (or babylon) to generate AST nodes.
gotcha The serializer expects nodes with a 'type' property; non-AST objects will not be serialized by this serializer. ↓
fix Make sure the snapshot content contains Babel AST nodes. For other objects, Jest's default serializer will be used.
deprecated The package is no longer actively maintained. There are no security issues reported, but bugs may remain unaddressed. ↓
fix Consider alternatives like jest-serializer-ast or writing your own serializer with a library like pretty-format.
gotcha Serializer may not show private properties (prefixed with underscore) or Babel node internal metadata (e.g., start/end locations) unless they are enumerable. ↓
fix If you need location data, configure Babel parser to include 'ranges' or check the node's 'loc' property.
Install
npm install jest-serializer-babel-ast yarn add jest-serializer-babel-ast pnpm add jest-serializer-babel-ast Imports
- jest-serializer-babel-ast wrong
import serializer from 'jest-serializer-babel-ast'correctAdd to jest.snapshotSerializers in package.json or jest.config.js: 'jest-serializer-babel-ast' - expect.addSnapshotSerializer wrong
const serializer = require('jest-serializer-babel-ast').defaultcorrectexpect.addSnapshotSerializer(require('jest-serializer-babel-ast')) - default (CommonJS) wrong
const { serialize } = require('jest-serializer-babel-ast')correctconst serializer = require('jest-serializer-babel-ast')
Quickstart
// jest.config.js or package.json
module.exports = {
snapshotSerializers: ['jest-serializer-babel-ast']
}
// test file
const { parse } = require('@babel/parser');
test('AST snapshot', () => {
const ast = parse('const x = 1;', { sourceType: 'module' });
expect(ast).toMatchSnapshot();
});
// Snapshot output will show structured AST instead of JSON.