Skip to content

GlobalWindow

David Ortner edited this page Oct 16, 2024 · 2 revisions

GlobalWindow represents a window instance detached from a Browser running on the global scope.

Each Window in Happy DOM is usually a Node.js Virtual Machine Context to be able to execute logic inside an isolated environment. GlobalWindow isn't a Node.js Virtual Machine Context and will instead run its logic on the global scope.

Running Happy DOM on the global scope can be useful for setting up a test environment running in its own Node process.

The package @happy-dom/global-registrator can be used to make this easier to setup.

Signature:

class GlobalWindow extends Window

Example

import { GlobalWindow } from 'happy-dom';

const window = new GlobalWindow();
const document = window.document;

document.write(`
   <script>
       globalThis.helloWorld = 'Hello world!';
   </script>
`);

// Outputs "Hello world!"
console.log(global.helloWorld);

// Close window
await window.happyDOM.close();
Clone this wiki locally