From fc6122682af3a74ddef6bc8c8c86e8af654b534b Mon Sep 17 00:00:00 2001 From: Max Milton Date: Tue, 16 Jan 2024 12:46:15 +1100 Subject: [PATCH] test: Fixes for new happy-dom API --- test/setup.ts | 10 ++++++++-- test/unit/utils.test.ts | 12 ++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/test/setup.ts b/test/setup.ts index cb3adbc..8c0fd0f 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -51,9 +51,15 @@ function setupMocks(): void { global.performance.measure = noop; } -export function reset(): void { +export async function reset(): Promise { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (global.happyDOM) { + await happyDOM.abort(); + window.close(); + } + setupDOM(); setupMocks(); } -reset(); +await reset(); diff --git a/test/unit/utils.test.ts b/test/unit/utils.test.ts index df94fba..5af9323 100644 --- a/test/unit/utils.test.ts +++ b/test/unit/utils.test.ts @@ -302,16 +302,17 @@ describe('onRemove', () => { expect(onRemove).toHaveLength(2); }); - test('calls callback when watched element is removed', () => { + test('calls callback when watched element is removed', async () => { const spy = mock(() => {}); const root = document.createElement('div'); document.body.appendChild(root); onRemove(root, spy); root.remove(); + await happyDOM.waitUntilComplete(); expect(spy).toHaveBeenCalledTimes(1); }); - test('calls callback when parent parent element is removed', () => { + test('calls callback when parent parent element is removed', async () => { const spy = mock(() => {}); const root = document.createElement('div'); const parent = document.createElement('div'); @@ -321,10 +322,11 @@ describe('onRemove', () => { document.body.appendChild(root); onRemove(child, spy); root.remove(); + await happyDOM.waitUntilComplete(); expect(spy).toHaveBeenCalledTimes(1); }); - test('does not call callback when nested child element is removed', () => { + test('does not call callback when nested child element is removed', async () => { const spy = mock(() => {}); const root = document.createElement('div'); const child = document.createElement('div'); @@ -332,10 +334,11 @@ describe('onRemove', () => { document.body.appendChild(root); onRemove(root, spy); child.remove(); + await happyDOM.waitUntilComplete(); expect(spy).not.toHaveBeenCalled(); }); - test('does not call callback when element is added or moved', () => { + test('does not call callback when element is added or moved', async () => { const spy = mock(() => {}); const root = document.createElement('div'); const child = document.createElement('div'); @@ -343,6 +346,7 @@ describe('onRemove', () => { onRemove(root, spy); root.appendChild(child); document.body.appendChild(root); + await happyDOM.waitUntilComplete(); expect(spy).not.toHaveBeenCalled(); }); });