Skip to content

Commit

Permalink
Wrote test that fails when runScripts: 'dangerously'
Browse files Browse the repository at this point in the history
- See modosc#392 for context
- Unintentionally discovered a potential bug: KEYS was never cleared
  • Loading branch information
DanKaplanSES committed Mar 1, 2024
1 parent 91004e5 commit 69421b7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/global-jsdom/esm/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 11 additions & 1 deletion packages/global-jsdom/test/jsdom.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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()
Expand Down
12 changes: 11 additions & 1 deletion packages/global-jsdom/test/jsdom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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()
Expand Down
12 changes: 11 additions & 1 deletion packages/global-jsdom/test/jsdom.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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()
Expand Down

0 comments on commit 69421b7

Please sign in to comment.