jsdom icon indicating copy to clipboard operation
jsdom copied to clipboard

BeforeUnloadEvent not available in window

Open wtho opened this issue 5 years ago • 1 comments
trafficstars

Basic info:

  • Node.js version: v13.11.0
  • jsdom version: v16.2.1

Minimal reproduction case

const { JSDOM } = require("jsdom");
const { window } = new JSDOM(``);
window.BeforeUnloadEvent; // undefined

How does similar code behave in browsers?

In Chrome v80: window.BeforeUnloadEvent // BeforeUnloadEvent() { [native code] }

MDN Documentation

https://developer.mozilla.org/en-US/docs/Web/API/BeforeUnloadEvent

wtho avatar Mar 22 '20 22:03 wtho

Quick & dirty solution:

window.BeforeUnloadEvent = class BeforeUnloadEvent extends CustomEvent {
    constructor() {
        super('beforeunload', { cancelable: true });
    }
};

michael-slx avatar Aug 19 '24 10:08 michael-slx