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

CypressError: cy.findByText() failed because it requires the subject be a global 'document' object. #114

Closed
leosuncin opened this issue Feb 2, 2020 · 6 comments · Fixed by #115
Labels
bug Something isn't working released

Comments

@leosuncin
Copy link
Contributor

leosuncin commented Feb 2, 2020

  • cypress-testing-library version: 5.2.0
  • node version: 13.7.0
  • npm (or yarn) version: 6.13.6

Relevant code or config

it('should validate the credentials', () => {
    cy.findByText(/Log me in/i)
      .click()
      .findByText(/Username should not be empty/i)
      .findByText(/Password should not be empty/i);
});

What you did: Bump @testing-library/cypress from 5.0.2 to 5.2.0

What happened:

  1) Register page should validate the data:                                                                                                                                                                                      [149/2152]
     CypressError: cy.findByText() failed because it requires the subject be a global 'document' object.                                                                                                                                    

The subject received was:

<span.MuiButton-label>

The previous command that ran was:

cy.click()

All 3 subject validations failed on this subject.
at Object.cypressErr (http://localhost:3000/__cypress/runner/cypress_runner.js:86207:11)
at Object.throwErr (http://localhost:3000/__cypress/runner/cypress_runner.js:86162:18)
at Object.throwErrByPath (http://localhost:3000/__cypress/runner/cypress_runner.js:86194:17)
at ensureDocument (http://localhost:3000/__cypress/runner/cypress_runner.js:74338:21)
at validateType (http://localhost:3000/__cypress/runner/cypress_runner.js:74184:16)
at Object.ensureSubjectByType (http://localhost:3000/__cypress/runner/cypress_runner.js:74210:9)
at pushSubjectAndValidate (http://localhost:3000/__cypress/runner/cypress_runner.js:81127:15)
at Context. (http://localhost:3000/__cypress/runner/cypress_runner.js:81413:18)
at http://localhost:3000/__cypress/runner/cypress_runner.js:80892:33
at tryCatcher (http://localhost:3000/__cypress/runner/cypress_runner.js:120203:23)
at Promise._settlePromiseFromHandler (http://localhost:3000/__cypress/runner/cypress_runner.js:118139:31)
at Promise._settlePromise (http://localhost:3000/__cypress/runner/cypress_runner.js:118196:18)
at Promise._settlePromiseCtx (http://localhost:3000/__cypress/runner/cypress_runner.js:118233:10)
at Async../node_modules/bluebird/js/release/async.js.Async._drainQueue (http://localhost:3000/__cypress/runner/cypress_runner.js:114933:12)
at Async../node_modules/bluebird/js/release/async.js.Async._drainQueues (http://localhost:3000/__cypress/runner/cypress_runner.js:114938:10)
at Async.drainQueues (http://localhost:3000/__cypress/runner/cypress_runner.js:114812:14)

https://github.com/leosuncin/mui-next-ts/pull/26/checks?check_run_id=421662750

Reproduction repository:

https://github.com/leosuncin/mui-next-ts/tree/dependabot/npm_and_yarn/testing-library/cypress-5.2.0

Problem description:

Suggested solution:

it('should validate the credentials', () => {
    cy.findByText(/Log me in/i)
      .click();
    cy.findByText(/Username should not be empty/i)
      .findByText(/Password should not be empty/i);
});
@NicholasBoll
Copy link
Contributor

Thanks for the detailed issue. I'll look into this.

The logic should be trying the previous subject first (in your case, the element containing "Log me in"). If the cy.findByText(/Username should not be empty/i) fails with that subject, it should retry with the original logic.

Something about that fallback doesn't seem to be working in your case and I'll look into why.

My best guess is this error is coming from Cypress and not the logic of this command. The command is registered to receive no subject, an element, the document or window. It seems to be receiving something other than those types and Cypress is throwing that error.

@NicholasBoll
Copy link
Contributor

I don't have quick access to an environment to test my theory. If you can manually edit the node_modules/@testing-library/cypress/dust/index.js file and remove the strings 'element', 'document', 'window' from the array at line https://github.com/testing-library/cypress-testing-library/blob/master/src/index.js#L47, save and restart Cypress, does that fix the issue?

@NicholasBoll NicholasBoll added the bug Something isn't working label Feb 2, 2020
@leosuncin
Copy link
Contributor Author

I don't have quick access to an environment to test my theory. If you can manually edit the node_modules/@testing-library/cypress/dust/index.js file and remove the strings 'element', 'document', 'window' from the array at line https://github.com/testing-library/cypress-testing-library/blob/master/src/index.js#L47, save and restart Cypress, does that fix the issue?

It works

@NicholasBoll
Copy link
Contributor

If you have time, feel free to open a pull request with that change. A test that verifies the existing failure and changed code passing, that be great.

If not, we can get to it on Monday.

@leosuncin
Copy link
Contributor Author

leosuncin commented Feb 3, 2020

If you have time, feel free to open a pull request with that change. A test that verifies the existing failure and changed code passing, that be great.

If not, we can get to it on Monday.

WIP

kentcdodds pushed a commit that referenced this issue Feb 3, 2020
* test: chain commands with subject different of document, element or window

* fix: set previous subject to be optional

This allows to commands to be chained with a previous command
that not returns a document, element or window

FIX #114
@kentcdodds
Copy link
Member

🎉 This issue has been resolved in version 5.2.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants