jestjs/jest · error · Error

jest-snapshot: Failed to parse ${sourceFilePath}

Error message

jest-snapshot: Failed to parse ${sourceFilePath}

What it means

Thrown by `processInlineSnapshotsWithBabel` in jest-snapshot utils.ts after `@babel/core`'s `parseSync` returns a falsy AST. Babel failed to produce a parse tree for the source file (the JSX-syntax-plugin recovery at lines 232-248 also failed, or the failure was unrelated to JSX). Without an AST the inline-snapshot patcher cannot locate `toMatchInlineSnapshot(...)` call sites, so Jest aborts.

Source

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

          // unique name to make sure Babel does not complain about a possible duplicate plugin.
          'JSX syntax plugin added by Jest snapshot',
        ];
        ast = parseSync(sourceFile, {
          filename: sourceFilePath,
          plugins: [...plugins, jsxSyntaxPlugin],
          presets,
          root: rootDir,
        });
      } catch {
        throw error;
      }
    } else {
      throw error;
    }
  }

  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');
        }

View on GitHub (pinned to f49721c78e)

Solutions

  1. Open the named sourceFilePath in an editor with babel-backed parsing and fix the reported syntax error.
  2. Ensure the Jest test environment's babel config (`babel.config.js`/`.babelrc`, or `transform` entry) includes presets for the syntax in use (e.g. `@babel/preset-typescript`, `@babel/preset-react`).
  3. If the parse error is from JSX, confirm `@babel/plugin-syntax-jsx` (or the preset) is resolvable in node_modules - the recovery path at utils.ts:232 only triggers on the specific missing-plugin error string.
  4. Run Jest with `--no-cache` after fixing babel config to avoid a stale cached transform.

Example fix

// before: babel.config.js missing the TS preset for a .ts test
module.exports = { presets: [['@babel/preset-env', { targets: { node: 'current' } }]] };
// after
module.exports = {
  presets: [
    ['@babel/preset-env', { targets: { node: 'current' } }],
    '@babel/preset-typescript',
    ['@babel/preset-react', { runtime: 'automatic' }],
  ],
};
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the test file parses before running with -u
const { parseSync } = require('@babel/core');
try {
  parseSync(sourceFilePath, { filename: sourceFilePath, babelrc: true, configFile: true });
} catch (e) { throw new Error('test file does not parse: ' + e.message); }

Try / catch

// Wrap test runner invocation; surface parse errors distinctly
try { await runJest(['-u']); }
catch (e) {
  if (/jest-snapshot: Failed to parse/.test(e.message))
    throw new Error('Fix syntax in the named test file before retrying');
  throw e;
}

Prevention

When it happens

Trigger: Running Jest in update mode (`-u`/`--ci=false` with new inline snapshots) on a test file that Babel cannot parse: syntax errors, unsupported syntax without the matching babel plugin, a `babel.config.js` whose target preset is missing, or a transform config that strips plugins Jest's snapshot patcher needs.

Common situations: Test file uses JSX/TSX/ decorators but the project's babel config does not include the needed plugin; a hand-edited test file left a stray syntax error; mixing TypeScript enum/satisfies/`using` syntax without `@babel/preset-typescript`; a custom transform produces output that Babel re-parsing rejects.

Related errors


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