Skip to content

VirtualConsolePrinter.addEventListener()

David Ortner edited this page Jan 21, 2025 · 4 revisions

Adds an event listener.

Signature

addEventListener(eventType: 'print' | 'clear', listener: (event: Event) => void): void;

Parameters

Parameter Type Description
eventType "print" | "clear" Event type.
listener (event: Event) => void): void Listener to add.

Returns

void

Examples

Print event

import { Window } from 'happy-dom';

const window = new Window();

window.happyDOM.virtualConsolePrinter.addEventListener('print', () => {
    const log = window.happyDOM.virtualConsolePrinter.readAsString();
    // Will output 'Test {"test": true}' to the NodeJS console
    global.console.log(log);
});

window.console.log('Test', { test: true });

Clear event

import { Window } from 'happy-dom';

const window = new Window();

window.happyDOM.virtualConsolePrinter.addEventListener('clear', () => {
    // Will clear the NodeJS log
    global.console.clear();
});

window.console.clear();
Clone this wiki locally