jest: [Bug]: Prettier v3 doesn't work with jest-snapshots
Version
29.6.1
Steps to reproduce
- Install
prettier@3.0.0
, which was just released yesterday - Write a test that would generate an inline snapshot, like:
test('snapshot', () => {
expect('abc').toMatchInlineSnapshot();
});
Expected behavior
The inline snapshot is updated
Actual behavior
Jest fails with
● Test suite failed to run
TypeError: prettier.resolveConfig.sync is not a function
at runPrettier (node_modules/jest-snapshot/build/InlineSnapshots.js:308:30)
Additional context
Prettier just released v3, which removed this line that added the sync
function to resolveConfig
. It looks like eslint-plugin-prettier
ran into a similar problem. based on this issue.
Environment
System:
OS: macOS 13.4.1
CPU: (10) arm64 Apple M1 Pro
Binaries:
Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
pnpm: 8.5.1 - ~/Library/pnpm/pnpm
npmPackages:
jest: ^29.6.0 => 29.6.1
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 41
- Comments: 25 (15 by maintainers)
Commits related to this issue
- fix: force use of Prettier v3 for formatting Addresses: - https://github.com/jestjs/jest/issues/14305#issuecomment-1668786780 Namely: > The suggestion above is a good idea, but it may have some pr... — committed to wincent/masochist by wincent a year ago
- fix(prettier): fix jest by disabling prettier Will have to run the script after updating the snapshots until the following issue is solved: https://github.com/jestjs/jest/issues/14305 — committed to nfroidure/metapak-nfroidure by nfroidure a year ago
- fix(prettier): fix jest by disabling prettier Will have to run the script after updating the snapshots until the following issue is solved: https://github.com/jestjs/jest/issues/14305 — committed to nfroidure/metapak-nfroidure by nfroidure a year ago
- fix(prettier): fix jest by disabling prettier Will have to run the script after updating the snapshots until the following issue is solved: https://github.com/jestjs/jest/issues/14305 — committed to nfroidure/metapak-nfroidure by nfroidure a year ago
- Chore: Remove inline snapshots (#1825) #### Details Due to https://github.com/jestjs/jest/issues/14305, our inline snapshots are causing problems when using `yarn test -u`. We discussed this int... — committed to microsoft/accessibility-insights-action by DaveTryon 9 months ago
- chore: Remove inline snapshots (#6976) #### Details Due to https://github.com/jestjs/jest/issues/14305, our inline snapshots are causing problems when using `yarn test -u`. We discussed this int... — committed to microsoft/accessibility-insights-web by DaveTryon 9 months ago
- fix!: prettier v3 does not work with jest-snapshot KK-1017. More details: https://github.com/jestjs/jest/issues/14305 — committed to City-of-Helsinki/kukkuu-admin by nikomakela 9 months ago
- fix!: prettier v3 does not work with jest-snapshot KK-1017. More details: https://github.com/jestjs/jest/issues/14305 — committed to City-of-Helsinki/kukkuu-admin by nikomakela 9 months ago
- Manually update snapshots The manual reason is because Prettier 3 isn't supported by Jest (yet). See [Jest docs](https://jestjs.io/docs/configuration/#prettierpath-string) and [this Jest tracking iss... — committed to octokit/oauth-methods.js by kfcampbell 8 months ago
- build(deps): lock file maintenance (#178) * build(deps): lock file maintenance * Use @gr2m's fetch-mock * Update octokit/types version * Manually update snapshots The manual reason is because Pre... — committed to octokit/oauth-methods.js by renovate[bot] 8 months ago
- fix!: prettier v3 does not work with jest-snapshot KK-1017. More details: https://github.com/jestjs/jest/issues/14305 — committed to City-of-Helsinki/kukkuu-admin by nikomakela 9 months ago
- fix!: prettier v3 does not work with jest-snapshot KK-1017. More details: https://github.com/jestjs/jest/issues/14305 — committed to City-of-Helsinki/kukkuu-admin by nikomakela 9 months ago
- fix: upgrade jest to v30.0.0-alpha.1 see https://github.com/jestjs/jest/issues/14305 — committed to lawvs/poi-plugin-quest-2 by lawvs 8 months ago
- chore(deps): update jest 29.7.0 -> 30.0.0-alpha2 The version is needed because currently jest snapshots cannot be used if prettier 3 is used. It has been fixed but is not released in a non-alpha vers... — committed to mikavilpas/parjs by barona-mika-vilpas 7 months ago
- chore: Upgrade jest to work with prettier 3 https://github.com/jestjs/jest/issues/14305 — committed to useVenice/venice by tonyxiao 7 months ago
It’ll be in Jest 30. Not sure when there’ll be a release, but within the next few weeks, hopefully 🙂
https://github.com/jestjs/jest/releases/tag/v30.0.0-alpha.1
That’s pretty much what
@prettier/sync
does, but doesn’t work due to https://github.com/prettier/prettier-synchronized/issues/4#issuecomment-1649355749Easiest would be to make
toMatchInlineSnapshot
async. But that’s horribly breaking 😅Seems like it is possible to add
prettierPath: null
to Jest config. Formatting can be done using Prettier outside of Jest run. That can be a workaround until solution will be found.Hum, do we really need that formatting? I always found it weird to format the contents of a template string with the code indentation since is is changing a const value in that code. In this case it is maybe a nice to have (but I think it is subjective), but considering it is a blocking feature, probably that getting rid of it won’t hurt that much.
What I did here in fact, not a big deal: https://github.com/nfroidure/metapak-nfroidure/blob/1ead42722e69c1db71d8c154718934ed32355b0e/src/eslint/package.test.ts#L156-L215
With the release of that, we should be able to integrate here.
I don’t have a firm date in mind, but I can probably do a prerelease.
Nightlies would be awesome (or CI publishing in general), but we don’t have that at the moment, unfortunately.
@SimenB Just curious about when/how the release process works to publish a new version with the changes merged in https://github.com/jestjs/jest/pull/14566, thanks!
The suggestion above is a good idea, but it may have some problematic side effects: if
prettier
andprettier-2
are installed indevDependencies
of the same package, some package managers (definitely yarn 1, likely others too) will nondeterministically choose whichprettier
binary (from v2 or v3) to link undernode_modules/.bin
. So it may appear to work at first, but then cause errors or mysterious inconsistencies in CI or on someone else’s computer.I wish I could think of a better way around this, but the best I can come up with is a postinstall script to fix the link if needed:
That module is currently broken (https://github.com/prettier/prettier-synchronized/pull/5), but I think it should work after that is released. We’ll then have to look at the version of prettier we load, and decide which API to use
I doubt we’ll ever support Prettier v3 as we need it to work synchronously.
However, we might be able to add support for
@prettier/sync
.