Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest-environment-jsdom: stop setting document to null on teardown #13972

Merged
merged 2 commits into from Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -84,7 +84,7 @@ describe('JSDomEnvironment', () => {

/**
* When used in conjunction with Custom Elements (part of the WebComponents standard)
* setting the global.document to null too early is problematic because:
* setting the `global` and `global.document` to null too early is problematic because:
*
* CustomElement's disconnectedCallback method is called when a custom element
* is removed from the DOM. The disconnectedCallback could need the document
Expand All @@ -94,7 +94,7 @@ describe('JSDomEnvironment', () => {
* The custom element will be removed from the DOM at this point, therefore disconnectedCallback
* will be called, so please make sure the global.document is still available at this point.
*/
it('should not set the global.document to null too early', () => {
it('should call CE disconnectedCallback with valid globals on teardown', () => {
const env = new JSDomEnvironment(
{
globalConfig: makeGlobalConfig(),
Expand All @@ -103,12 +103,46 @@ describe('JSDomEnvironment', () => {
{console, docblockPragmas: {}, testPath: __filename},
);

const originalCloseFn = env.global.close.bind(env.global);
env.global.close = () => {
originalCloseFn();
expect(env.global.document).not.toBeNull();
};
let hasDisconnected = false;
let documentWhenDisconnected = null;

return env.teardown();
// define a custom element
const {HTMLElement} = env.global;
class MyCustomElement extends HTMLElement {
disconnectedCallback() {
hasDisconnected = true;
documentWhenDisconnected = env.global.document;
}
}

// append an instance of the custom element
env.global.customElements.define('my-custom-element', MyCustomElement);
const instance = env.global.document.createElement('my-custom-element');
env.global.document.body.appendChild(instance);

// teardown will disconnect the custom elements
env.teardown();

expect(hasDisconnected).toBe(true);
expect(documentWhenDisconnected).not.toBeNull();
});

it('should not fire load event after the environment was teared down', async () => {
const env = new JSDomEnvironment(
{
globalConfig: makeGlobalConfig(),
projectConfig: makeProjectConfig(),
},
{console, docblockPragmas: {}, testPath: __filename},
);

const loadHandler = jest.fn();
env.global.document.addEventListener('load', loadHandler);
env.teardown();

// The `load` event is fired in microtasks, wait until the microtask queue is reliably flushed
await new Promise(resolve => setTimeout(resolve, 0));

expect(loadHandler).not.toHaveBeenCalled();
});
});
7 changes: 0 additions & 7 deletions packages/jest-environment-jsdom/src/index.ts
Expand Up @@ -160,13 +160,6 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
this.global.removeEventListener('error', this.errorEventListener);
}
this.global.close();

// Dispose "document" to prevent "load" event from triggering.

// Note that this.global.close() will trigger the CustomElement::disconnectedCallback
// Do not reset the document before CustomElement disconnectedCallback function has finished running,
// document should be accessible within disconnectedCallback.
Object.defineProperty(this.global, 'document', {value: null});
}
this.errorEventListener = null;
// @ts-expect-error: this.global not allowed to be `null`
Expand Down