jest: Prettier is used by `toMatchInlineSnapshot`, but it is not a dependency of `jest`
🐛 Bug Report
Prettier is used by jest for toMatchInlineSnapshot
, but since it’s not a dependency of jest
, toMatchInlineSnapshot
only works if some other package depends on prettier (or it’s in the user’s package.json
).
To Reproduce
Steps to reproduce the behavior:
- Make a new directory and run
yarn init --yes
inside it - Run
yarn add jest
- Create a test using the
toMatchInlineSnapshot
matcher, eg:expect(2 + 2).toMatchInlineSnapshot()
- Notice that the test fails with an error indicating that prettier cannot be found.
Expected behavior
Test passes with no issues
npx envinfo --preset jest
System:
OS: macOS 10.14.4
CPU: x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
Binaries:
Node: 12.1.0 - /usr/local/bin/node
Yarn: 1.13.0 - /usr/local/bin/yarn
npm: 6.9.0 - /usr/local/bin/npm
npmPackages:
jest: 24.8.0 => 24.8.0
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 34
- Comments: 16 (9 by maintainers)
Commits related to this issue
- Install prettier, but only to support 'toMatchInlineSnapshot' in jest tests https://github.com/facebook/jest/issues/8467#issuecomment-617459378 — committed to johnnyoshika/prisma-nexus-postgres by johnnyoshika 3 years ago
- create snapshot test case & add prettier for using 'toMatchInlineSnapshot' ref: https://github.com/facebook/jest/issues/8467 — committed to linnefromice/graphql_practice by linnefromice 3 years ago
looks like it is supposed to throw a descriptive error message:
https://github.com/facebook/jest/blob/fa4cbbf87cb3c97fe44f1fee30663715a9dbba71/packages/jest-snapshot/src/inline_snapshots.ts#L33-L35
The error message that I get with
jest-cli@25.4.0
is:Oh, nice!
yeah, we should definitely to that as a first step regardless. #7744
Hmm. Not sure; thanks for the context.
Maybe it should be listed as a peerdep? A descriptive error message saying “you need to install prettier” might be good enough, too. It’s unexpected to get a “could not find prettier” error the first time you use
.toMatchInlineSnapshot
.Not sure if this makes sense for jest, but prettier could be replaced with babel + recast here, and it could use prettier for printing (instead of recast) as an optimization, when it’s available.
I just ran into this as well.
Maybe the error message should at least state that
prettier
must be installed to usetoMatchInlineSnapshot
. I just spent way too long wondering what was throwing the error. I definitely did not think jest was using a package without requiring it as a dependency. That seems like an obvious code smell.Would you expect jest to bundle some version and defer to a local one? Related #7792