{"id":"4e2816047f149c21","repo":"jestjs/jest","slug":"could-not-infer-prettier-parser-for-file-sourcef","errorCode":null,"errorMessage":"Could not infer Prettier parser for file ${sourceFilePath}","messagePattern":"Could not infer Prettier parser for file (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-snapshot/src/InlineSnapshots.ts","lineNumber":129,"sourceCode":") => {\n  // Resolve project configuration.\n  // For older versions of Prettier, do not load configuration.\n  const config = prettier.resolveConfig\n    ? prettier.resolveConfig.sync(sourceFilePath, {editorconfig: true})\n    : null;\n\n  // Prioritize parser found in the project config.\n  // If not found detect the parser for the test file.\n  // For older versions of Prettier, fallback to a simple parser detection.\n  // @ts-expect-error - `inferredParser` is `string`\n  const inferredParser: PrettierParserName | null | undefined =\n    (typeof config?.parser === 'string' && config.parser) ||\n    (prettier.getFileInfo\n      ? prettier.getFileInfo.sync(sourceFilePath).inferredParser\n      : simpleDetectParser(sourceFilePath));\n\n  if (!inferredParser) {\n    throw new Error(\n      `Could not infer Prettier parser for file ${sourceFilePath}`,\n    );\n  }\n\n  // Snapshots have now been inserted. Run prettier to make sure that the code is\n  // formatted, except snapshot indentation. Snapshots cannot be formatted until\n  // after the initial format because we don't know where the call expression\n  // will be placed (specifically its indentation), so we have to do two\n  // prettier.format calls back-to-back.\n  return prettier.format(\n    prettier.format(sourceFileWithSnapshots, {\n      ...config,\n      filepath: sourceFilePath,\n    }),\n    {\n      ...config,\n      filepath: sourceFilePath,\n      parser: createFormattingParser(snapshotMatcherNames, inferredParser),","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-snapshot/src/InlineSnapshots.ts#L111-L147","documentation":"Thrown by `runPrettier` in jest-snapshot (InlineSnapshots.ts:128-132) when neither Prettier's config, `getFileInfo`, nor the fallback `simpleDetectParser` could infer a parser for the source file. Jest runs Prettier on test files after writing inline snapshots; if Prettier can't identify the language it can't format, so Jest bails.","triggerScenarios":"Using `toMatchInlineSnapshot` in a test file whose extension maps to no Prettier parser (e.g. `.svelte`, `.vue` without plugin, `.mjs` in older Prettier, custom extension). Prettier config that overrides `parser` to a value the file can't satisfy.","commonSituations":"Writing inline snapshots in TypeScript/JSX-adjacent or framework-specific files that need a Prettier plugin. Stale Prettier config that disabled the parser. CI using a different Prettier version than local.","solutions":["Install/configure the appropriate Prettier plugin for the file type (e.g. `prettier-plugin-svelte`).","Override the parser in `.prettierrc` for the matching glob, or rename the test file to an extension Prettier recognizes (`.ts`, `.js`, `.tsx`).","Pin Prettier to a version whose `getFileInfo` recognizes the extension.","Use a non-inline `toMatchSnapshot` (writes to `*.snap` and skips Prettier) if formatting the source file isn't feasible."],"exampleFix":"// before — test in Counter.svelte.test that Prettier can't parse\nexpect(c).toMatchInlineSnapshot();\n\n// after — move inline snapshot into a .ts wrapper or configure parser\n// .prettierrc\n// { \"overrides\": [{ \"files\": \"*.svelte\", \"options\": { \"parser\": \"svelte\" } }] }","handlingStrategy":"validation","validationCode":"const prettier = require('prettier');\nconst info = prettier.getFileInfo?.sync(testFilePath) ?? { inferredParser: null };\nif (!info.inferredParser) {\n  throw new Error(`No Prettier parser for ${testFilePath}; configure one or rename.`);\n}","typeGuard":"function hasInferredParser(prettier: any, filePath: string): boolean {\n  if (!prettier.getFileInfo) return true;\n  return Boolean(prettier.getFileInfo.sync(filePath).inferredParser);\n}","tryCatchPattern":"try {\n  expect(x).toMatchInlineSnapshot();\n} catch (e) {\n  if (e instanceof Error && /Could not infer Prettier parser/.test(e.message)) {\n    // fall back to non-inline snapshot\n    expect(x).toMatchSnapshot();\n  } else throw e;\n}","preventionTips":["Keep your Prettier config in sync with the file types your tests use.","Install Prettier plugins for any non-standard extension before authoring inline snapshots in it.","Prefer `.ts`/`.tsx` test files for inline snapshots to guarantee parser support."],"tags":["inline-snapshot","prettier","formatting"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}