capricorn86 / happy-dom

A JavaScript implementation of a web browser without its graphical user interface
MIT License
3.08k stars 185 forks source link

Lots of NPE errors logged by MutationListener #1436

Closed thaggie closed 1 month ago

thaggie commented 1 month ago

We're using @swc/jest with @happy-dom/jest-environment@14.7.1 / happy-dom@14.7.1, (upgrading to X.10.2 doesn't seem to help).

We're seeing a lot of errors logged from MutationListener which is causing a lot of noise in our unit test logs.

[my-project]:     console.error
[my-project]:       TypeError: Cannot read properties of null (reading 'length')
[my-project]:           at /builds/my-project/node_modules/happy-dom/src/mutation-observer/MutationListener.ts:55:16
[my-project]:           at /builds/my-project/node_modules/happy-dom/src/window/BrowserWindow.ts:1021:49
[my-project]:           at Function.captureError (/builds/my-project/node_modules/happy-dom/src/window/WindowErrorUtility.ts:29:13)
[my-project]:           at Timeout._onTimeout (/builds/my-project/node_modules/happy-dom/src/window/BrowserWindow.ts:1021:24)
[my-project]:           at listOnTimeout (node:internal/timers:569:17)
[my-project]:           at processTimers (node:internal/timers:512:7)
[my-project]: 
[my-project]:       at Function.dispatchError (node_modules/happy-dom/src/window/WindowErrorUtility.ts:57:45)
[my-project]:       at Function.captureError (node_modules/happy-dom/src/window/WindowErrorUtility.ts:31:9)
[my-project]:       at Timeout._onTimeout (node_modules/happy-dom/src/window/BrowserWindow.ts:1021:24)
capricorn86 commented 1 month ago

Thank you for reporting @thaggie! :slightly_smiling_face:

It seems like some code continues to run after the environment was shut down (and the Window and MutationObservers has been destroyed).

I have added a fix that checks if it has been destroyed, which should solve your problem.

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