capricorn86 / happy-dom

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

Window can not be created in Bun from version 14.5.1 and higher #1415

Open Dyskal opened 2 months ago

Dyskal commented 2 months ago

Describe the bug When using new Window() in a Bun environment, the script crash

To Reproduce Steps to reproduce the behavior:

  1. Install happy-dom version 14.5.1 or higher
  2. Import Window and run new Window()
  3. Run the script with bun bun ./index.js
  4. See error

Expected behavior The program should run correctly

Screenshots If applicable, add screenshots to help explain your problem.

Device:

Additional context

26 |                 resolve();
27 |             }
28 |             else {
29 |                 this.readyStateCallbacks.push(resolve);
30 |                 if (this.totalTasks === 0 && !this.immediate) {
31 |                     this.immediate = this.window.requestAnimationFrame(this.endTask.bind(this));
                                          ^
TypeError: this.window.requestAnimationFrame is not a function. (In 'this.window.requestAnimationFrame(this.endTask.bind(this))', 'this.window.requestAnimationFrame' is undefined)
      at ${PROJECT_ROOT}\node_modules\happy-dom\lib\nodes\document\DocumentReadyStateManager.js:31:38
      at new Promise (:1:21)
      at waitUntilComplete (${PROJECT_ROOT}\node_modules\happy-dom\lib\nodes\document\DocumentReadyStateManager.js:24:16)
      at new BrowserWindow (${PROJECT_ROOT}\node_modules\happy-dom\lib\window\BrowserWindow.js:545:9)
      at new Window (${PROJECT_ROOT}\node_modules\happy-dom\lib\window\Window.js:36:9)
Dyskal commented 2 months ago

Also reproductible on bun 1.1.4 in wsl2

twlite commented 1 month ago

I am also having the same problem but in my case I see two errors. I am using happy-dom@14.11.0

26 |                 resolve();
27 |             }
28 |             else {
29 |                 this.readyStateCallbacks.push(resolve);
30 |                 if (this.totalTasks === 0 && !this.immediate) {
31 |                     this.immediate = this.window.requestAnimationFrame(this.endTask.bind(this));
                                          ^
TypeError: this.window.requestAnimationFrame is not a function. (In 'this.window.requestAnimationFrame(this.endTask.bind(this))', 'this.window.requestAnimationFrame' is undefined)
      at PROJECT_ROOT\node_modules\happy-dom\lib\nodes\document\DocumentReadyStateManager.js:31:38
      at new Promise (:1:21)
      at waitUntilComplete (PROJECT_ROOT\node_modules\happy-dom\lib\nodes\document\DocumentReadyStateManager.js:24:16)
      at new BrowserWindow (PROJECT_ROOT\node_modules\happy-dom\lib\window\BrowserWindow.js:554:9)
      at new Window (PROJECT_ROOT\node_modules\happy-dom\lib\window\Window.js:36:9)
      at createDocument (PROJECT_ROOT\src\document.ts:12:18)
      at new UI (PROJECT_ROOT\src\index.ts:9:29)
      at PROJECT_ROOT\test\index.tsx:6:12
184 |             return false;
185 |         }
186 |         if (ancestorNode === referenceNode) {
187 |             return true;
188 |         }
189 |         if (!ancestorNode[PropertySymbol.childNodes].length) {
                   ^
TypeError: undefined is not an object (evaluating 'ancestorNode[PropertySymbol.childNodes].length')
      at isInclusiveAncestor (PROJECT_ROOT\node_modules\happy-dom\lib\nodes\node\NodeUtility.js:189:14)
      at appendChild (PROJECT_ROOT\node_modules\happy-dom\lib\nodes\node\NodeUtility.js:24:52)
      at appendChild (PROJECT_ROOT\node_modules\happy-dom\lib\nodes\element\ElementUtility.js:52:13)
      at insertNode (PROJECT_ROOT\src\document.ts:48:9)
      at insertExpression (PROJECT_ROOT\node_modules\solid-js\universal\dist\universal.js:85:9)
      at PROJECT_ROOT\node_modules\solid-js\universal\dist\universal.js:224:9
      at createRoot (PROJECT_ROOT\node_modules\solid-js\dist\server.js:58:14)
      at render (PROJECT_ROOT\node_modules\solid-js\universal\dist\universal.js:222:7)
      at render (PROJECT_ROOT\src\index.ts:33:5)
      at PROJECT_ROOT\test\index.tsx:18:16

Bun v1.1.8 (Windows x64)
yaroslav-ilin commented 1 month ago

I think it's up to the Bun.js team to fix this, they seem to be aware:

There is a bug with this value for contextified options not having the correct prototype.

Source: https://bun.sh/docs/runtime/nodejs-apis#node-vm

Probably, this is the issue https://github.com/oven-sh/bun/issues/4205.