diff --git a/packages/happy-dom/src/window/Window.ts b/packages/happy-dom/src/window/Window.ts index bfe5aa110..8bdd57ff7 100644 --- a/packages/happy-dom/src/window/Window.ts +++ b/packages/happy-dom/src/window/Window.ts @@ -806,14 +806,22 @@ export default class Window extends EventTarget implements IWindow { ); } - this.dispatchEvent( - new MessageEvent('message', { - data: message, - origin: this.parent.location.origin, - source: this.parent, - lastEventId: '' - }) - ); + const setImmediate = + globalThis.setImmediate || (setTimeout); + const clearImmediate = + globalThis.clearImmediate || (clearTimeout); + const taskId = this.happyDOM.asyncTaskManager.startTask(() => clearImmediate(immediateId)); + const immediateId = setImmediate(() => { + this.happyDOM.asyncTaskManager.endTask(taskId); + this.dispatchEvent( + new MessageEvent('message', { + data: message, + origin: this.parent.location.origin, + source: this.parent, + lastEventId: '' + }) + ); + }); } /**