Skip to content

Global Registrator

David Ortner edited this page Feb 9, 2026 · 5 revisions

About

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

The properties of GlobalWindow is applied in the global scope.

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>`;

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

or register on import with default settings:

import '@happy-dom/global-registrator/register.js';

// Settings can be accessed and modified via the global "happyDOM" object
window.happyDOM.setURL('http://localhost:3000');
window.happyDOM.settings.happyDOM.settings.navigator.maxTouchPoints = 0;

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

// Outputs: "My button"
console.log(document.querySelector('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