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

[new rule] no-snapshots #312

Closed
cartogram opened this issue Jul 19, 2019 · 10 comments · Fixed by #575
Closed

[new rule] no-snapshots #312

cartogram opened this issue Jul 19, 2019 · 10 comments · Fixed by #575
Labels

Comments

@cartogram
Copy link
Contributor

Wondering if I could contribute this no-snapshots rule that I wrote for Shopify

@SimenB
Copy link
Member

SimenB commented Jul 19, 2019

For sure, I bet more people would like to use it. Thanks!

@benmonro
Copy link
Contributor

this is already supported by just setting the maxSize to zero on no-large-snapshots

@NullDivision
Copy link

What about a parameter that would still allow snapshots as long as other non-snapshot assertions are present? I'm not completely against snapshots as a guide to how stuff should look overall but I am against them if there are no assertions for actual elements.

@github-actions
Copy link

🎉 This issue has been resolved in version 23.11.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@markogresak
Copy link

markogresak commented Jun 6, 2021

this is already supported by just setting the maxSize to zero on no-large-snapshots

@benmonro I was hoping for that, but it's not true.

Here are the problems:

  1. We have to set up linting of .snap files. In multi-project workspaces, like one managed by Nx, it can be tedious to correctly configure each new project (e.g. a simple lib module). I don't want to chase after developers to fix the config.
  2. The feedback cycle is too long. I want the devs who use ESLint editor integration to see the error on toMatchSnapshot while they are writing the test, not after running the test to generate the .snap file and then having to run eslint to spot the error.
  3. The error message does not explain the problem at all, so it can be confusing to other developers.
    The config:
    "rules": {
      "jest/no-large-snapshots": ["error", { "maxSize": 0 }]
    }
    
    Error reported by ESLint:
    3:1  error  `103`s should begin with lowercase  jest/no-large-snapshots
    
    Is that a bug in the code? The error is better with { "maxSize": 1 }:
    3:1  error  Expected Jest snapshot to be smaller than 1 lines but was 103 lines long  jest/no-large-snapshots
    

I believe the no-snapshots rule serves a different rule than no-large-snapshots. It forbids the usage of toMatchSnapshot, while no-large-snapshots allows snapshot testing, but moderates the snapshot size.

For reference, the URL to docs linked by @cartogram above is broken, it should be this: https://github.com/Shopify/web-configs/blob/main/packages/eslint-plugin/docs/rules/jest/no-snapshots.md

@benmonro would you consider reopening this issue and adding no-snapshots rule? I'm happy to pick up the development work and send a PR if @cartogram is not around to do it.

@G-Rath
Copy link
Collaborator

G-Rath commented Jun 6, 2021

@markogresak it was actually me who closed the issue, via implementing no-restricted-matchers (#575) which'll let you do exactly what you want :)

@markogresak
Copy link

Thanks for a quick answer, on a Sunday no less!

I have completely missed that part since I was focused on disallowing snapshot matching.

For anyone else looking for this, here's how to set it up to work exactly like the no-snapshots rule:

"jest/no-restricted-matchers": [
  "error",
  {
    "toMatchSnapshot": "Do not use toMatchSnapshot or related methods that generate jest snapshots.",
    "toMatchInlineSnapshot": "Do not use toMatchInlineSnapshot or related methods that generate jest snapshots.",
    "toThrowErrorMatchingSnapshot": "Do not use toThrowErrorMatchingSnapshot or related methods that generate jest snapshots.",
    "toThrowErrorMatchingInlineSnapshot": "Do not use toThrowErrorMatchingInlineSnapshot or related methods that generate jest snapshots."
  }
]

@benmonro
Copy link
Contributor

benmonro commented Jun 6, 2021

Bless you for banning snapshot tests. Are they even tests at all?

@markogresak
Copy link

I would say not really 😄 I see it as an lazy alternative to explicitly matching for the expected properties. The latter takes a bit more time, but it's easier for future devs to read and understand the assertions.

@benmonro
Copy link
Contributor

benmonro commented Jun 6, 2021

I found them to even be counter productive. Make a small change and break 50 unrelated snapshot tests. Then you either -u or waste time chasing ghosts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
6 participants