simonsmith/cypress-image-snapshot

CypressError: cy.screenshot() failed because it requires a DOM element

Nek0-hinata opened this issue · 3 comments

Hi,
When i migrating cypress-image-snapshot to your package (also migrate cypress version 9.0 to 10.0), i find some strange error as the image shown after.
image

It happened on all my cyress test which used cy.matchImageSnapshot() command and the stack trace point out it happend because of elementToScreenshot at file src/command.ts 70:5.

I'm confuesed about this line. When the prevSubjet is optional, the subject could be null. On this condition, how to use cy.wrap() to do a screenshot.

I dont know what to do next. Am I made some mistake while migration?

For example, this is how I used in my repo
image

This does seem related to #40 as well. It might be necessary to add some tests for this kind of usage and see if they fail. Contributions are welcome otherwise I will do some investigation when I get some time

I've struggled to reproduce this. As you can see in the tests the plugin works fine with and without a subject (so either cy.matchImageSnapshot() or cy.get('something').matchImageSnapshot()

https://github.com/simonsmith/cypress-image-snapshot/blob/master/cypress/e2e/matchImageSnapshot.cy.ts#L6-L14

This behaviour is part of Cypress described here - https://docs.cypress.io/api/cypress-api/custom-commands#Optional-with-Types

In the case of this plugin I allow everything:

  Cypress.Commands.add(
    'matchImageSnapshot',
    {
      prevSubject: ['optional', 'element', 'document', 'window'],
    },
    matchImageSnapshot(defaultOptionsOverrides),
  )

If you can provide a concrete way for me to reproduce this (even if it's a fork of the tests in this repo) I'll reopen and investigate