Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Jest uses the wrong Prettier parser for Flow files, leading to unparsable code #12929

Closed
captbaritone opened this issue Jun 9, 2022 · 5 comments · Fixed by #13323
Closed

Comments

@captbaritone
Copy link
Contributor

captbaritone commented Jun 9, 2022

Version

28.1.1 (latest)

Steps to reproduce

git clone git@github.com:captbaritone/jest-flow-prettier-issue.git
cd jest-flow-prettier-issue
yarn
yarn repro # jest -u

Expected behavior

The snapshot updates and the rest of the file is left untouched.

Actual behavior

The snapshot updates and line 3 of test.js is updated to remove the quotes. Now Flow can't parse that file.

Additional context

When updating inline snapshots Jest applies Prettier. Unfortunatly with Flow files, Jest disregards the parser defined in the user's Pretteir config and instead uses an infered one for that file.

In the case of Flow files, Jest seems to pick incorrectly, and instead uses babel.

This leads prettier to think it's okay to remove quotes around numeric object keys.

Note that Prettier never unquotes numeric property names in Angular expressions, TypeScript, and Flow because the distinction between string and numeric keys is significant in these languages

-- Prettier Docs

The result is that Jest writes back a snapshot file which Prettier cannot parse, and so even subsequent Prettier runs cannot restore the file to a parseable state.

The only recourse is to manually update all numeric keys in the test file (and to be very careful not to ever update the inline snapshot automatically ever again).

The bug is caused on this line of jest-snapshot where the inferred parser is preferred over one explicitly defined in the config. Note how Prettier itself prefers the explicit parser defined in the config over inferring the parser code pointer.

Environment

System:
    OS: macOS 12.2.1
    CPU: (10) arm64 Apple M1 Pro
  Binaries:
    Node: 17.3.0 - ~/.nvm/versions/node/v17.3.0/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v17.3.0/bin/yarn
    npm: 8.3.0 - ~/.nvm/versions/node/v17.3.0/bin/npm
  npmPackages:
    jest: ^28.1.1 => 28.1.1
@captbaritone
Copy link
Contributor Author

@SimenB
Copy link
Member

SimenB commented Jun 9, 2022

Any idea how to fix this?

@github-actions
Copy link

github-actions bot commented Jul 9, 2022

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Jul 9, 2022
@SimenB SimenB added Pinned and removed Stale labels Jul 9, 2022
@JVBorges
Copy link
Contributor

Hi, can I take a look at this issue?

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants