From 69421b76ad6af5d5e576409cec5df75a4e543a3d Mon Sep 17 00:00:00 2001 From: Daniel Kaplan Date: Thu, 29 Feb 2024 23:12:05 -0800 Subject: [PATCH] Wrote test that fails when runScripts: 'dangerously' - See #392 for context - Unintentionally discovered a potential bug: KEYS was never cleared --- packages/global-jsdom/esm/index.mjs | 5 ++++- packages/global-jsdom/test/jsdom.test.cjs | 12 +++++++++++- packages/global-jsdom/test/jsdom.test.js | 12 +++++++++++- packages/global-jsdom/test/jsdom.test.mjs | 12 +++++++++++- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/global-jsdom/esm/index.mjs b/packages/global-jsdom/esm/index.mjs index 6bd8ab4..eb2450f 100644 --- a/packages/global-jsdom/esm/index.mjs +++ b/packages/global-jsdom/esm/index.mjs @@ -50,7 +50,10 @@ export default function globalJsdom(html = defaultHtml, options = {}) { // add access to our jsdom instance global.$jsdom = jsdom - const cleanup = () => KEYS.forEach((key) => delete global[key]) + const cleanup = () => { + KEYS.forEach((key) => delete global[key]) + KEYS.length = 0 + } document.destroy = cleanup diff --git a/packages/global-jsdom/test/jsdom.test.cjs b/packages/global-jsdom/test/jsdom.test.cjs index 0b30b42..ff99ac0 100644 --- a/packages/global-jsdom/test/jsdom.test.cjs +++ b/packages/global-jsdom/test/jsdom.test.cjs @@ -4,7 +4,7 @@ describe('jsdom-global', () => { beforeEach(() => global.document && global.document.destroy && global.document.destroy()) afterEach(() => global.document && global.document.destroy && global.document.destroy()) - const expectedKeys = ['document', 'alert', 'requestAnimationFrame'] + const expectedKeys = ['document', 'alert', 'requestAnimationFrame', 'NodeList'] describe('initialization and cleanup', () => { it('works', () => { @@ -16,6 +16,16 @@ describe('jsdom-global', () => { }) }) + describe('initialization and cleanup with `scripts: dangerously`', () => { + it('works', () => { + expectedKeys.forEach((k) => expect(global[k]).to.be.undefined) + const cleanup = jsdom(undefined, { runScripts: 'dangerously' }) + expectedKeys.forEach((k) => expect(global[k]).to.exist) + cleanup() + expectedKeys.forEach((k) => expect(global[k]).to.be.undefined) + }) + }) + describe('dom', () => { it('works', () => { jsdom() diff --git a/packages/global-jsdom/test/jsdom.test.js b/packages/global-jsdom/test/jsdom.test.js index 0b30b42..ff99ac0 100644 --- a/packages/global-jsdom/test/jsdom.test.js +++ b/packages/global-jsdom/test/jsdom.test.js @@ -4,7 +4,7 @@ describe('jsdom-global', () => { beforeEach(() => global.document && global.document.destroy && global.document.destroy()) afterEach(() => global.document && global.document.destroy && global.document.destroy()) - const expectedKeys = ['document', 'alert', 'requestAnimationFrame'] + const expectedKeys = ['document', 'alert', 'requestAnimationFrame', 'NodeList'] describe('initialization and cleanup', () => { it('works', () => { @@ -16,6 +16,16 @@ describe('jsdom-global', () => { }) }) + describe('initialization and cleanup with `scripts: dangerously`', () => { + it('works', () => { + expectedKeys.forEach((k) => expect(global[k]).to.be.undefined) + const cleanup = jsdom(undefined, { runScripts: 'dangerously' }) + expectedKeys.forEach((k) => expect(global[k]).to.exist) + cleanup() + expectedKeys.forEach((k) => expect(global[k]).to.be.undefined) + }) + }) + describe('dom', () => { it('works', () => { jsdom() diff --git a/packages/global-jsdom/test/jsdom.test.mjs b/packages/global-jsdom/test/jsdom.test.mjs index b5defb9..eab3e95 100644 --- a/packages/global-jsdom/test/jsdom.test.mjs +++ b/packages/global-jsdom/test/jsdom.test.mjs @@ -5,7 +5,7 @@ describe('jsdom-global', () => { beforeEach(() => global.document && global.document.destroy && global.document.destroy()) afterEach(() => global.document && global.document.destroy && global.document.destroy()) - const expectedKeys = ['document', 'alert', 'requestAnimationFrame'] + const expectedKeys = ['document', 'alert', 'requestAnimationFrame', 'NodeList'] describe('initialization and cleanup', () => { it('works', () => { @@ -17,6 +17,16 @@ describe('jsdom-global', () => { }) }) + describe('initialization and cleanup with `scripts: dangerously`', () => { + it('works', () => { + expectedKeys.forEach((k) => expect(global[k]).to.be.undefined) + const cleanup = jsdom(undefined, { runScripts: 'dangerously' }) + expectedKeys.forEach((k) => expect(global[k]).to.exist) + cleanup() + expectedKeys.forEach((k) => expect(global[k]).to.be.undefined) + }) + }) + describe('dom', () => { it('works', () => { jsdom()