{"id":"9ea39aac321f3eb5","repo":"jestjs/jest","slug":"jest-snapshot-failed-to-parse-sourcefilepath","errorCode":null,"errorMessage":"jest-snapshot: Failed to parse ${sourceFilePath}","messagePattern":"jest-snapshot: Failed to parse (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-snapshot/src/utils.ts","lineNumber":255,"sourceCode":"          // unique name to make sure Babel does not complain about a possible duplicate plugin.\n          'JSX syntax plugin added by Jest snapshot',\n        ];\n        ast = parseSync(sourceFile, {\n          filename: sourceFilePath,\n          plugins: [...plugins, jsxSyntaxPlugin],\n          presets,\n          root: rootDir,\n        });\n      } catch {\n        throw error;\n      }\n    } else {\n      throw error;\n    }\n  }\n\n  if (!ast) {\n    throw new Error(`jest-snapshot: Failed to parse ${sourceFilePath}`);\n  }\n  traverseAst(snapshots, ast, snapshotMatcherNames);\n\n  return {\n    snapshotMatcherNames,\n    sourceFile,\n    // substitute in the snapshots in reverse order, so slice calculations aren't thrown off.\n    sourceFileWithSnapshots: snapshots.reduceRight(\n      (sourceSoFar, nextSnapshot) => {\n        const {node} = nextSnapshot;\n        if (\n          !node ||\n          typeof node.start !== 'number' ||\n          typeof node.end !== 'number'\n        ) {\n          throw new Error('Jest: no snapshot insert location found');\n        }\n","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-snapshot/src/utils.ts#L237-L273","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Open the named sourceFilePath in an editor with babel-backed parsing and fix the reported syntax error.","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`).","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.","Run Jest with `--no-cache` after fixing babel config to avoid a stale cached transform."],"exampleFix":"// before: babel.config.js missing the TS preset for a .ts test\nmodule.exports = { presets: [['@babel/preset-env', { targets: { node: 'current' } }]] };\n// after\nmodule.exports = {\n  presets: [\n    ['@babel/preset-env', { targets: { node: 'current' } }],\n    '@babel/preset-typescript',\n    ['@babel/preset-react', { runtime: 'automatic' }],\n  ],\n};","handlingStrategy":"validation","validationCode":"// Confirm the test file parses before running with -u\nconst { parseSync } = require('@babel/core');\ntry {\n  parseSync(sourceFilePath, { filename: sourceFilePath, babelrc: true, configFile: true });\n} catch (e) { throw new Error('test file does not parse: ' + e.message); }","typeGuard":null,"tryCatchPattern":"// Wrap test runner invocation; surface parse errors distinctly\ntry { await runJest(['-u']); }\ncatch (e) {\n  if (/jest-snapshot: Failed to parse/.test(e.message))\n    throw new Error('Fix syntax in the named test file before retrying');\n  throw e;\n}","preventionTips":["Run an editor/typecheck pass that uses babel on test files so parse errors surface before Jest.","Keep babel preset/plugin versions aligned with the syntax you use.","Run `--no-cache` after changing babel config to avoid stale parse results."],"tags":["jest-snapshot","babel","inline-snapshot","parse-error"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}