Jest: Prettier is used by `toMatchInlineSnapshot`, but it is not a dependency of `jest`

Created on 14 May 2019  路  10Comments  路  Source: facebook/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
Bug Report Needs Repro Needs Triage

Most helpful comment

Oh, nice!

A descriptive error message saying "you need to install prettier" might be good enough, too

yeah, we should definitely to that as a first step regardless. #7744

All 10 comments

This is expected. @azz might remember the particulars

Would you expect jest to bundle some version and defer to a local one? Related #7792

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.

There's a bunch of discussion in the original PR #6380, especially https://github.com/facebook/jest/pull/6380#issuecomment-395107886.

The main thing we wanna avoid is to completely butcher the styling of the test file creating churn. Using prettier assures we're consistent with the current styling, which is nice. Not sure how well using recast would work? It might work great without butchering existent styling at all for all I know 馃檪

Yup, that's what it's designed for :+1: recast keeps track of which AST nodes have been changed and only prints those ones using original styling. It uses the original source for all the other nodes. You can defer its parsing to another parser, so babel could be used to ensure all the necessary syntax and etc is supported.

Oh, nice!

A descriptive error message saying "you need to install prettier" might be good enough, too

yeah, we should definitely to that as a first step regardless. #7744

I just ran into this as well.

Still no progress?

Maybe the error message should at least state that prettier must be installed to use toMatchInlineSnapshot. 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.

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 [email protected] is:

Cannot find module 'prettier' from 'setup_jest_globals.js'
Was this page helpful?
0 / 5 - 0 ratings