Skip to content

Commit

Permalink
fix: use setup hook to init the environment
Browse files Browse the repository at this point in the history
In jest v28 it seems the constructor doesn't work fine, so I've moved the init of jsdom to the setup hook instead.
  • Loading branch information
mario-beltran-cs authored and simon360 committed May 20, 2022
1 parent d9190d6 commit d25f6e6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
7 changes: 5 additions & 2 deletions __tests__/environment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const MockEnvironment = function () {

const MockDefaultEnvironment = jest.fn(MockEnvironment);
MockDefaultEnvironment.prototype.teardown = jest.fn();
MockDefaultEnvironment.prototype.setup = jest.fn();

describe("using jest-environment-jsdom", () => {
let jestEnvironmentJSDOMGlobal;
Expand All @@ -39,9 +40,11 @@ describe("using jest-environment-jsdom", () => {
expect(mockJestEnvironmentJsdom).toHaveBeenCalledTimes(1);
});

test("should set jsdom on its global object", () => {
test("should set jsdom on its global object", async () => {
const environment = new jestEnvironmentJSDOMGlobal();

await environment.setup()

expect(environment.global.jsdom).toBe(mockDom);
});

Expand All @@ -50,6 +53,6 @@ describe("using jest-environment-jsdom", () => {

environment.teardown();

expect(environment.global.jsdom).toBe(null);
expect(environment.global.jsdom).toBe(undefined);
});
});
12 changes: 5 additions & 7 deletions environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ if (pkg.default) {
}

module.exports = class JSDOMEnvironmentGlobal extends JSDOMEnvironment {
constructor(config, options) {
super(config, options);

async setup() {
await super.setup();
this.global.jsdom = this.dom;
}

teardown() {
this.global.jsdom = null;

return super.teardown();
async teardown() {
this.global.jsdom = undefined;
await super.teardown();
}
};

0 comments on commit d25f6e6

Please sign in to comment.