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

Cannot read property '_listeners' of undefined #464

Closed
iamzapata opened this issue May 2, 2022 · 2 comments · Fixed by #477
Closed

Cannot read property '_listeners' of undefined #464

iamzapata opened this issue May 2, 2022 · 2 comments · Fixed by #477
Assignees
Labels
bug Something isn't working high priority

Comments

@iamzapata
Copy link

Hi, first of all, thank you so much for this library!

I have this method on a a class that is a wrapper on a DOM element. When I call the method in my tests, I can an Cannot read property '_listeners' of undefined error.

code:

  destroy(): void {
    removeEventListener('click', this.handleCloseDialog)
    this.element.remove()
    this.onCloseDialog()
  }

error:

TypeError: Cannot read property '_listeners' of undefined
 ❯ removeEventListener node_modules/happy-dom/lib/event/EventTarget.js:27:18
 ❯ ReleaseDialog.destroy src/components/ReleaseDialog/ReleaseDialog.component.ts:89:4
     87| 
     88|   destroy(): void {
     89|     removeEventListener('click', this.handleCloseDialog)
       |    ^
     90|     this.element.remove()
     91|     this.onCloseDialog()

If I change my code to:

  destroy(): void {
-  removeEventListener('click', this.handleCloseDialog)
+  window.removeEventListener('click', this.handleCloseDialog)
    this.element.remove()
    this.onCloseDialog()
  }

The error disappears. Is it possible to not require the window prefix, or maybe also allow globalThis instead of window ?

@capricorn86 capricorn86 added bug Something isn't working high priority labels May 16, 2022
@capricorn86 capricorn86 self-assigned this May 16, 2022
capricorn86 added a commit that referenced this issue May 16, 2022
…veEventListener() not working when called in the global context.
capricorn86 added a commit that referenced this issue May 16, 2022
…y-_listeners-of-undefined

#464@patch: Fixes issue with methods like addEventListener() and remo…
@capricorn86
Copy link
Owner

Thanks for reporting @iamzapata! 🙂

I have fixed the problem.

You can read more about the release here:
https://github.com/capricorn86/happy-dom/releases/tag/v3.2.2

@iamzapata
Copy link
Author

@capricorn86 thank you!

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

Successfully merging a pull request may close this issue.

2 participants