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

When using an invalid selector, the error isn't processed properly by waitFor #1259

Closed
julienw opened this issue Sep 14, 2023 · 5 comments · Fixed by #1261 or #1269
Closed

When using an invalid selector, the error isn't processed properly by waitFor #1259

julienw opened this issue Sep 14, 2023 · 5 comments · Fixed by #1261 or #1269

Comments

@julienw
Copy link
Contributor

julienw commented Sep 14, 2023

  • @testing-library/dom version: I think it is 8.20.1 (used by @testing-library/react 13.4.0)
  • Testing Framework and version: react 13.4.0
  • jest 28.1.3
  • DOM Environment: jsdom 19.0.0

Relevant code or config:

screen.getByText('foo', { selector: '[href^=blob:]' });

The selector is incorrect and should be:

screen.getByText('foo', { selector: '[href^="blob:"]' });

What you did:

What happened:

$ npm test

> bug@0.9.0 test
> jest

  console.error
    Error: Uncaught [TypeError: Cannot set property message of  which has only a getter]
        at reportException (/home/julien/travail/git/bug-testing-library/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:66:24)
        at Timeout.task [as _onTimeout] (/home/julien/travail/git/bug-testing-library/node_modules/jsdom/lib/jsdom/browser/Window.js:519:9)
        at listOnTimeout (node:internal/timers:569:17)
        at processTimers (node:internal/timers:512:7) {
      detail: TypeError: Cannot set property message of  which has only a getter
          at onTimeout (/home/julien/travail/git/bug-testing-library/node_modules/@testing-library/react/node_modules/@testing-library/dom/dist/wait-for.js:21:19)
          at handleTimeout (/home/julien/travail/git/bug-testing-library/node_modules/@testing-library/react/node_modules/@testing-library/dom/dist/wait-for.js:159:14)
          at Timeout.task [as _onTimeout] (/home/julien/travail/git/bug-testing-library/node_modules/jsdom/lib/jsdom/browser/Window.js:514:19)
          at listOnTimeout (node:internal/timers:569:17)
          at processTimers (node:internal/timers:512:7),
      type: 'unhandled exception'
    }

      at VirtualConsole.<anonymous> (node_modules/jest-environment-jsdom/build/index.js:70:23)
      at reportException (node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:70:28)
      at Timeout.task [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:519:9)

 FAIL  ./file.test.js (5.467 s)
  ✕ reproduces the error (5008 ms)

  ● reproduces the error

    TypeError: Cannot set property message of  which has only a getter

      at onTimeout (node_modules/@testing-library/react/node_modules/@testing-library/dom/dist/wait-for.js:21:19)
      at handleTimeout (node_modules/@testing-library/react/node_modules/@testing-library/dom/dist/wait-for.js:159:14)
      at Timeout.task [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:514:19)

  ● reproduces the error

    thrown: "Exceeded timeout of 5000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

       5 | import {render, screen} from '@testing-library/react'
       6 |
    >  7 | test('reproduces the error', async () => {
         | ^
       8 |   render(<a href="http://example.org">foo</a>);
       9 |   await screen.findByText('foo', {selector: '[href^=http:]'});
      10 | });

      at Object.test (file.test.js:7:1)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        5.719 s, estimated 6 s
Ran all test suites.

Reproduction:

I couldn't reproduce in codesandbox but here is an example:

https://github.com/julienw/bug-testing-library

  1. git clone https://github.com/julienw/bug-testing-library
  2. cd bug-testing-library
  3. npm i
  4. npm test

Problem description:

We don't have a good error, instead we have a timeout.
The actual error is in a console error message above, which isn't ideal.

I haven't tried with newer versions of jsdom and jest yet.

Suggested solution:

I'm not sure to be honest...

@julienw julienw changed the title When using an invalid selector, the error isn't processed properly When using an invalid selector, the error isn't processed properly by waitFor Sep 14, 2023
@julienw
Copy link
Contributor Author

julienw commented Sep 15, 2023

The same thing happens with the latest versions:
@testing-library/react: 14.0.0
@testing-library/dom: 9.3.1
jest: 29.7.0
jest-environment-jsdom: 29.7.0 (using jsdom 20.0.3 under the hood)

I updated the repository.

@julienw
Copy link
Contributor Author

julienw commented Sep 15, 2023

I believe the problem happens in

error.message = getConfig().getElementError(
error.message,
container,
).message
return error

The message property of a DOMException can't be 'set'. Maybe we should use defineProperty instead?

@github-actions
Copy link

🎉 This issue has been resolved in version 9.3.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

eps1lon pushed a commit to eps1lon/dom-testing-library that referenced this issue Oct 3, 2023
eps1lon added a commit that referenced this issue Oct 3, 2023
…ssage directly (#1268)

Co-authored-by: Julien Wajsberg <felash@gmail.com>
Fixes #1259
@github-actions
Copy link

github-actions bot commented Oct 3, 2023

🎉 This issue has been resolved in version 10.0.0-alpha.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link

github-actions bot commented Apr 4, 2024

🎉 This issue has been resolved in version 10.0.0-alpha.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@MatanBobi MatanBobi reopened this Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants