Skip to content

Global Registrator

David Ortner edited this page Apr 4, 2024 · 3 revisions

About

@happy-dom/global-registrator is a utility that registers Happy DOM globally, which makes it easy to setup your own test environment.

Installation

npm install @happy-dom/global-registrator --save-dev

Usage

Register

Calling GlobalRegistrator.register() creates a Window instance and applies the properties of the Window instance to the global scope.

import { GlobalRegistrator } from '@happy-dom/global-registrator';

GlobalRegistrator.register({ url: 'http://localhost:3000', width: 1920, height: 1080 });

document.body.innerHTML = `<button>My button</button>`;

const button = document.querySelector('button');

// Outputs: "My button"
console.log(button.innerText);

Unregister

Calling GlobalRegistrator.unregister() closes/destroyes the Window instance and restores properties to the same values as they where before calling GlobalRegistrator.register().

import { GlobalRegistrator } from '@happy-dom/global-registrator';

GlobalRegistrator.register();

await GlobalRegistrator.unregister();

// Outputs: "undefined"
console.log(global.document);
Clone this wiki locally