jestjs/jest · error · Error

Could not infer Prettier parser for file ${sourceFilePath}

Error message

Could not infer Prettier parser for file ${sourceFilePath}

What it means

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.

Source

Thrown at packages/jest-snapshot/src/InlineSnapshots.ts:129

) => {
  // Resolve project configuration.
  // For older versions of Prettier, do not load configuration.
  const config = prettier.resolveConfig
    ? prettier.resolveConfig.sync(sourceFilePath, {editorconfig: true})
    : null;

  // Prioritize parser found in the project config.
  // If not found detect the parser for the test file.
  // For older versions of Prettier, fallback to a simple parser detection.
  // @ts-expect-error - `inferredParser` is `string`
  const inferredParser: PrettierParserName | null | undefined =
    (typeof config?.parser === 'string' && config.parser) ||
    (prettier.getFileInfo
      ? prettier.getFileInfo.sync(sourceFilePath).inferredParser
      : simpleDetectParser(sourceFilePath));

  if (!inferredParser) {
    throw new Error(
      `Could not infer Prettier parser for file ${sourceFilePath}`,
    );
  }

  // Snapshots have now been inserted. Run prettier to make sure that the code is
  // formatted, except snapshot indentation. Snapshots cannot be formatted until
  // after the initial format because we don't know where the call expression
  // will be placed (specifically its indentation), so we have to do two
  // prettier.format calls back-to-back.
  return prettier.format(
    prettier.format(sourceFileWithSnapshots, {
      ...config,
      filepath: sourceFilePath,
    }),
    {
      ...config,
      filepath: sourceFilePath,
      parser: createFormattingParser(snapshotMatcherNames, inferredParser),

View on GitHub (pinned to f49721c78e)

Solutions

  1. Install/configure the appropriate Prettier plugin for the file type (e.g. `prettier-plugin-svelte`).
  2. Override the parser in `.prettierrc` for the matching glob, or rename the test file to an extension Prettier recognizes (`.ts`, `.js`, `.tsx`).
  3. Pin Prettier to a version whose `getFileInfo` recognizes the extension.
  4. Use a non-inline `toMatchSnapshot` (writes to `*.snap` and skips Prettier) if formatting the source file isn't feasible.

Example fix

// before — test in Counter.svelte.test that Prettier can't parse
expect(c).toMatchInlineSnapshot();

// after — move inline snapshot into a .ts wrapper or configure parser
// .prettierrc
// { "overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }] }
Defensive patterns

Strategy: validation

Validate before calling

const prettier = require('prettier');
const info = prettier.getFileInfo?.sync(testFilePath) ?? { inferredParser: null };
if (!info.inferredParser) {
  throw new Error(`No Prettier parser for ${testFilePath}; configure one or rename.`);
}

Type guard

function hasInferredParser(prettier: any, filePath: string): boolean {
  if (!prettier.getFileInfo) return true;
  return Boolean(prettier.getFileInfo.sync(filePath).inferredParser);
}

Try / catch

try {
  expect(x).toMatchInlineSnapshot();
} catch (e) {
  if (e instanceof Error && /Could not infer Prettier parser/.test(e.message)) {
    // fall back to non-inline snapshot
    expect(x).toMatchSnapshot();
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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