jestjs/jest · error · Error

Jest: no snapshot insert location found

Error message

Jest: no snapshot insert location found

What it means

Thrown inside the inline-snapshot source-reconstruction loop (utils.ts:266-272) when a matched AST node is missing numeric `start`/`end` byte offsets. Babel normally attaches location info to every node; a node without `start`/`end` cannot be spliced back into the source string, so Jest aborts rather than corrupting the file.

Source

Thrown at packages/jest-snapshot/src/utils.ts:271

  if (!ast) {
    throw new Error(`jest-snapshot: Failed to parse ${sourceFilePath}`);
  }
  traverseAst(snapshots, ast, snapshotMatcherNames);

  return {
    snapshotMatcherNames,
    sourceFile,
    // substitute in the snapshots in reverse order, so slice calculations aren't thrown off.
    sourceFileWithSnapshots: snapshots.reduceRight(
      (sourceSoFar, nextSnapshot) => {
        const {node} = nextSnapshot;
        if (
          !node ||
          typeof node.start !== 'number' ||
          typeof node.end !== 'number'
        ) {
          throw new Error('Jest: no snapshot insert location found');
        }

        // A hack to prevent unexpected line breaks in the generated code
        node.loc!.end.line = node.loc!.start.line;

        return (
          sourceSoFar.slice(0, node.start) +
          generate(node, {retainLines: true}).code.trim() +
          sourceSoFar.slice(node.end)
        );
      },
      sourceFile,
    ),
  };
};

export const processPrettierAst = (
  ast: File,

View on GitHub (pinned to f49721c78e)

Solutions

  1. Audit custom babel plugins in the transform/babel config and ensure they preserve node location info (`{ ...path.node }` patterns can drop positions).
  2. Align `@babel/core`, `@babel/generator`, and `@babel/types` to a single resolved version (`npm ls @babel/core`).
  3. If unavoidable, avoid inline snapshots in the affected file (use `toMatchSnapshot` writing to a `.snap` file instead).
Defensive patterns

Strategy: type-guard

Validate before calling

// If authoring a babel plugin in the transform chain, assert nodes carry positions
const hasLoc = node => typeof node?.start === 'number' && typeof node?.end === 'number';
if (!hasLoc(callExprNode)) throw new Error('plugin dropped node positions');

Type guard

const nodeHasPositions = (n: any): boolean =>
  n != null && typeof n.start === 'number' && typeof n.end === 'number';

Prevention

When it happens

Trigger: Hit when an inline-snapshot call-expression node returned by `traverseAst` has `node.start` or `node.end` that is not a number. This typically requires a non-standard babel plugin or transform that strips/removes location info from generated nodes, or a manually-constructed AST passed through `generate()`.

Common situations: A custom babel plugin in the transform pipeline that emits synthesized nodes without positions; an outdated `@babel/core`/`@babel/generator` version mismatch; a transform that rewrites call expressions and drops `loc`/`start`/`end`. Rarely seen with stock configurations.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/b2911ac517c8f613.json. Report an issue: GitHub.