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

fix: prevent crashing on elements without parentElement #871

Conversation

AriPerkkio
Copy link
Contributor

What:
Fixes #870.

Why:
Utility method waitForElementToBeRemoved should not crash when given element is already removed from its parent element. It should handle these cases and clearly report element is already removed.

How:
Check whether element has .parentElement before accessing it. Prevents unexpected null pointer.

Checklist:

  • Documentation added to the N/A
    docs site
  • Tests
  • Typescript definitions updated N/A
  • Ready to be merged

I was unable to use the pre-commit hook. The kcd-scripts test --findRelatedTests keeps running forever. After 10-15mins I cancelled it and commited with --no-verify. Same thing has happened to me before with @testing-library/eslint-plugin-jest-dom repository.

Here's output of the test case without the fix applied just to indicate the error is same as in #870.

Test case error
 FAIL   dom  src/__tests__/wait-for-element-to-be-removed.js
  ✕ requires element's parent to exist first (8 ms)

  ● requires element's parent to exist first

    expect(received).rejects.toThrowErrorMatchingInlineSnapshot(snapshot)

    Snapshot name: `requires element's parent to exist first 1`

    Snapshot: "The element(s) given to waitForElementToBeRemoved are already removed. waitForElementToBeRemoved requires that the element(s) exist(s) before waiting for removal."
    Received: "Cannot read property 'parentElement' of null"

      54 |   return expect(
      55 |     waitForElementToBeRemoved(div),
    > 56 |   ).rejects.toThrowErrorMatchingInlineSnapshot(
         |             ^
      57 |     `"The element(s) given to waitForElementToBeRemoved are already removed. waitForElementToBeRemoved requires that the element(s) exist(s) before waiting for removal."`,
      58 |   )
      59 | })

      at Object.toThrowErrorMatchingInlineSnapshot (node_modules/expect/build/index.js:241:20)
      at Object.<anonymous> (src/__tests__/wait-for-element-to-be-removed.js:56:13)

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 12, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 36c6d63:

Sandbox Source
react-testing-library-examples Configuration
dom-testing-library/issues/870 Issue #870

@codecov
Copy link

codecov bot commented Jan 12, 2021

Codecov Report

Merging #871 (36c6d63) into master (cb70902) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #871   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           26        26           
  Lines          945       946    +1     
  Branches       290       291    +1     
=========================================
+ Hits           945       946    +1     
Impacted Files Coverage Δ
src/wait-for-element-to-be-removed.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cb70902...36c6d63. Read the comment docs.

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Don't tell the TypeScript police we had this bug

@eps1lon eps1lon merged commit 6d32457 into testing-library:master Jan 12, 2021
@eps1lon
Copy link
Member

eps1lon commented Jan 12, 2021

@all-contributors add @AriPerkkio for bug, code

@allcontributors
Copy link
Contributor

@eps1lon

I've put up a pull request to add @AriPerkkio! 🎉

@github-actions
Copy link

🎉 This PR is included in version 7.29.3 🎉

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

waitForElementToBeRemoved: TypeError: Cannot read property 'parentElement' of null
2 participants