Skip to content
This repository has been archived by the owner on Nov 3, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ianschmitz committed Nov 28, 2019
1 parent 40c06be commit be76406
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/__tests__/index.test.ts
Expand Up @@ -22,4 +22,20 @@ describe("JSDomEnvironment", () => {
expect(typeof timer).toBe("number");
});
});

it("sets jsdom instance options", () => {
const href = "http://foo.com/";
instance = new JSDomEnvironment({ testURL: href } as any);

expect(instance!.global.location.href).toBe(href);
});

it("sets resource loader options", () => {
const userAgent = "some agent";
instance = new JSDomEnvironment({
testEnvironmentOptions: { userAgent },
} as any);

expect(instance!.global.navigator.userAgent).toBe(userAgent);
});
});
15 changes: 10 additions & 5 deletions src/index.ts
@@ -1,10 +1,10 @@
import { Script } from "vm";
import { Global, Config } from "@jest/types";
import { Config, Global } from "@jest/types";
import { installCommonGlobals } from "jest-util";
import mock, { ModuleMocker } from "jest-mock";
import { ModuleMocker } from "jest-mock";
import { JestFakeTimers as FakeTimers } from "@jest/fake-timers";
import { JestEnvironment, EnvironmentContext } from "@jest/environment";
import { JSDOM, VirtualConsole } from "jsdom";
import { EnvironmentContext, JestEnvironment } from "@jest/environment";
import { JSDOM, ResourceLoader, VirtualConsole } from "jsdom";

// The `Window` interface does not have an `Error.stackTraceLimit` property, but
// `JSDOMEnvironment` assumes it is there.
Expand All @@ -23,11 +23,16 @@ class JSDOMEnvironment implements JestEnvironment {
moduleMocker: ModuleMocker | null;

constructor(config: Config.ProjectConfig, options: EnvironmentContext = {}) {
// This handles advanced configurations like `userAgent`
// https://github.com/jsdom/jsdom#advanced-configuration
const resourceLoader = new ResourceLoader(config.testEnvironmentOptions);

this.dom = new JSDOM("<!DOCTYPE html>", {
pretendToBeVisual: true,
runScripts: "dangerously",
url: config.testURL,
virtualConsole: new VirtualConsole().sendTo(options.console || console),
resources: resourceLoader,
...config.testEnvironmentOptions,
});
const global = (this.global = this.dom.window.document.defaultView as Win);
Expand Down Expand Up @@ -71,7 +76,7 @@ class JSDOMEnvironment implements JestEnvironment {
return originalRemoveListener.apply(this, args);
};

this.moduleMocker = new mock.ModuleMocker(global as any);
this.moduleMocker = new ModuleMocker(global as any);

const timerConfig = {
idToRef: (id: number) => id,
Expand Down

0 comments on commit be76406

Please sign in to comment.