meinaart / cypress-plugin-snapshots

Plugin for snapshot tests in Cypress.io
MIT License
497 stars 117 forks source link

(feat) add inline snapshot #59

Open lukeapage opened 5 years ago

lukeapage commented 5 years ago

Having the assertion separate from the test makes it less likely to be reviewed and makes it harder to align assertion value and test.

Describe the solution you'd like To implement toMatchInlineSnapshot like jest.

Jest implementation: https://github.com/facebook/jest/commit/d3a6a7400a89b857a63d939fc5664ae8a2dfbbb2#diff-5d8368ae467702f37535e03f31a2ed96

It seems doable from a cursory look. I would follow the jest approach, requiring prettier if it’s used. I’m interested in implementing this myself, just raising this in case you have objections or suggestions.

lukeapage commented 5 years ago

jest captures the stack on calling toMatchInlineSnapshot and then matches up the ast based on the callsite column and line.

There are some challenges doing this with cypress:

  1. the call is async. We'd need to monket patch cypress so the stack trace comes from the sync part of the call rather than being called once async dependencies are fetched. This could be worked around I think, maybe a bit like this: https://github.com/cypress-io/cypress/issues/881#issuecomment-485235225
  2. the source is transformed. Unfortunately theres no way to get the sourcemapping right now, so we are dependent on this issue being resolved first: https://github.com/cypress-io/cypress/pull/4041

so, I think a workable approach would be to use the test title and the order of the calls to determine the right call to make. This is more limiting, but in practice I can't think of any problems - for instance if you have a forEach around the test and dynamically create the test - this currently works in jest, but it opts out if the same call site is used more than twice - so in practice it only works if the dynamic test name is created only once. The same for abstracted functions - they work but only if called once. So I do not think the limitations are too bad - it would work for me.

sidharthv96 commented 3 years ago

Now that https://github.com/cypress-io/cypress/pull/4041 is merged, is it technically possible to implement this?

jaredatdannyronsrescue commented 2 years ago

This would be a long awaited feature for us.