cypress-plugin-snapshots: Error when running all snapshots
Hi, thanks for the amazing plugin!!!
There is a possible bug, as when I run all tests in the cypress browser I receive following error:
CypressError: cy.task('cypress-plugin-snapshot:matchText') failed with the following error:
> Error: Cannot load snapshot file. File "__snapshots__/__all.snap does not contain valid JSON or javascript
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 24 (7 by maintainers)
Commits related to this issue
- Fixing snapshot filenames when running all tests (Fixes #10) Fixes #10 — committed to meinaart/cypress-plugin-snapshots by meinaart 6 years ago
- Breaking snapshot functionality when running all tests, previous fix breaks other functionality (Reopens #10) (Fixes #14) — committed to meinaart/cypress-plugin-snapshots by deleted user 5 years ago
- Breaking snapshot functionality when running all tests, previous fix breaks other functionality (Reopens #10) (Fixes #14) — committed to meinaart/cypress-plugin-snapshots by deleted user 5 years ago
- adds caveat section; mentions issue #10 adds caveat section which highlights #10 cypress-io/cypress/3090 — committed to meinaart/cypress-plugin-snapshots by deleted user 5 years ago
So the workaround I can offer for now is to fix
Cypress.spec
object by looking at the__filename
in each spec file. Here is the code to place intocypress/support/index.js
. It is a utility function that formsCypress.spec
from passed filenameFrom each spec file (that wants to use snapshots)
I have been playing with this in https://github.com/bahmutov/objection-example using this approach to run all specs with snapshots
This solution works for me universally with TypeScript:
support/snapshotPatch.ts
:support/index.ts
:IMO this issue should be mentioned on the README as it’s a huge blocker for me, I only found out after implementing and writing tests (using
.only
while writing tests) to find the error in the GUI test runner after I ran all my tests which made all my work previously useless.Please consider adding an eye-catching warning or ‘caveat’ section which mentions this issue so that other users of this package will not share the same experience I had or at the very least, know of this important bug.
Hope Cypress gets the fix in soon, :fingers
(added a PR as a suggestion for a new section in the README file: https://github.com/meinaart/cypress-plugin-snapshots/pull/37)
Especially for Typescript users, it’s easier to use a custom command for the workaround like this:
add it to the index.d.ts like this:
and then in the spec in a
beforeEach
:Seems not to be working if you are using TypeScript.
The maintenance of this package is freezed… I was added to the project but I didn’t have any right for merge PR or update project settings (CICD is broken)…
I experienced the same thing, to not have “ypress” folder, you juste have to remove
substr(1)
😉There is no reason to manually specify the path:
cy.fixCypressSpec(__filename);
I will remove the fix, it was causing other issues and the real reason is a bug in Cypress: https://github.com/cypress-io/cypress/issues/3090
Could this plugin run the fix (workaround) internally?
Thanks @dploeger and @441N143G, that’s really helpful.
Btw, TypeScript users will also want to install
@types/node
and add it to theirtsconfig
in order for__filename
to work:Also, @krisraven, I have had the same. I believe the issue here is that you may have provided
cypress/integration/mytest_spec.ts
instead of/cypress/integration/mytest_spec.ts
(no leading slash). This is also the case when using__filename
. With this argument, the one line in the workaround which truncates the first character is not required anymore:When I apply @bahmutov’s workaround - it works well, thanks for this 😄 however, after running the all the test specs, a folder called “ypress” gets created (ie.
ypress/integration/__snapshots__/myTestSpecName.spec.js.snap
). I was wondering if this is correct or is a side effect of the workaround, as it has not been mentioned here.I put the fix suggested by @bahmutov and it seems to work. But it looks like an error is thrown from getSpec.js,
if (Cypress.spec.absolute === '__all') { throw new Error('cypress-plugin-snapshots does not work when running all tests, this will be fixed once this bug is resolved: https://github.com/cypress-io/cypress/issues/3090'); }
If I comment this out the test works fine. The question is how do you handle this for others on the team? Tell them to npm install then go into node_modules and comment out the offending line? Is anyone else doing something more streamlined?
Any light at the end of this tunnel? The suggested fixes don’t seem to work for me 😦
Typescript users, just pass the exact spec file path to
beforeEach(fixCypressSpec('/cypress/integration/spec.ts'))
. 👍FYI @CSchulz @meinaart