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

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ianschmitz committed Mar 16, 2019
0 parents commit 9069f61
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
lib/
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"trailingComma": "es5"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Ian Schmitz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# jest-environment-jsdom-fourteen

Jest environment using JSDOM 14, which does not support Node 6 ([and will therefore not be used in Jest any time soon](https://github.com/kentcdodds/dom-testing-library/issues/115#issuecomment-428314737)). If you would like to use JSDOM 13, see https://github.com/theneva/jest-environment-jsdom-thirteen.

If you need a newer JSDOM than the one that ships with Jest, install this package using `npm install --save-dev jest-environment-jsdom-fourteen` or `yarn add jest-environment-jsdom-fourteen --dev`, and edit your Jest config like so:

```json
{
"testEnvironment": "jest-environment-jsdom-fourteen"
}
```
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "jest-environment-jsdom-fourteen",
"version": "0.1.0",
"repository": "https://github.com/ianschmitz/jest-environment-jsdom-fourteen",
"license": "MIT",
"main": "lib/index.js",
"files": [
"lib"
],
"dependencies": {
"jest-mock": "^24.5.0",
"jest-util": "^24.5.0",
"jsdom": "^14.0.0"
},
"scripts": {
"test": "jest",
"build": "rimraf lib && tsc"
},
"description": "JSDOM environment for Jest with JSDOM 14",
"author": "Ian Schmitz <ianschmitz@gmail.com>",
"devDependencies": {
"@types/jest": "^24.0.11",
"@types/node": "^11.11.3",
"husky": "^1.3.1",
"jest": "^24.1.0",
"mkdirp": "^0.5.1",
"prettier": "^1.16.4",
"pretty-quick": "^1.10.0",
"rimraf": "^2.6.3",
"ts-jest": "^24.0.0",
"typescript": "~3.3.3333"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
}
}
17 changes: 17 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

const JSDomEnvironment = jest.requireActual("../index");

describe("JSDomEnvironment", () => {
it("should configure setTimeout/setInterval to use the browser api", () => {
const env1 = new JSDomEnvironment({});

env1.fakeTimers.useFakeTimers();

const timer1 = env1.global.setTimeout(() => {}, 0);
const timer2 = env1.global.setInterval(() => {}, 0);

[timer1, timer2].forEach(timer => {
expect(typeof timer).toBe("number");
});
});
});
113 changes: 113 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { Script } from "vm";
import { ModuleMocker } from "jest-mock";
import { FakeTimers, installCommonGlobals } from "jest-util";
import { JSDOM, VirtualConsole } from "jsdom";

interface EnvironmentOptions {
console?: Object;
resources?: "usable" | object;
}

class JSDomEnvironment {
dom: any;
fakeTimers: any;
global: any;
errorEventListener?: Function;
moduleMocker?: ModuleMocker;

constructor(config: jest.ProjectConfig, options: EnvironmentOptions = {}) {
this.dom = new JSDOM(
"<!DOCTYPE html>",
Object.assign(
{
pretendToBeVisual: true,
runScripts: "dangerously",
url: config.testURL,
virtualConsole: new VirtualConsole().sendTo(
options.console || console
),
resources: options.resources,
},
config.testEnvironmentOptions
)
);

this.global = this.dom.window.document.defaultView;
// Node's error-message stack size is limited at 10, but it's pretty useful
// to see more than that when a test fails.
this.global.Error.stackTraceLimit = 100;
installCommonGlobals(this.global, config.globals);

// Report uncaught errors.
this.errorEventListener = event => {
if (userErrorListenerCount === 0 && event.error) {
process.emit("uncaughtException", event.error);
}
};
this.global.addEventListener("error", this.errorEventListener);

// However, don't report them as uncaught if the user listens to 'error' event.
// In that case, we assume the might have custom error handling logic.
const originalAddListener = this.global.addEventListener;
const originalRemoveListener = this.global.removeEventListener;
let userErrorListenerCount = 0;
this.global.addEventListener = function(name) {
if (name === "error") {
userErrorListenerCount++;
}
return originalAddListener.apply(this, arguments);
};
this.global.removeEventListener = function(name) {
if (name === "error") {
userErrorListenerCount--;
}
return originalRemoveListener.apply(this, arguments);
};

this.moduleMocker = new ModuleMocker(this.global);

const timerConfig = {
idToRef: (id: number) => id,
refToId: (ref: number) => ref,
};

this.fakeTimers = new FakeTimers({
config,
global: this.global,
moduleMocker: this.moduleMocker,
timerConfig,
});
}

setup(): Promise<void> {
return Promise.resolve();
}

teardown(): Promise<void> {
if (this.fakeTimers) {
this.fakeTimers.dispose();
}
if (this.global) {
if (this.errorEventListener) {
this.global.removeEventListener("error", this.errorEventListener);
}
// Dispose "document" to prevent "load" event from triggering.
Object.defineProperty(this.global, "document", { value: undefined });
this.global.close();
}
this.errorEventListener = undefined;
this.global = undefined;
this.dom = undefined;
this.fakeTimers = undefined;
return Promise.resolve();
}

runScript(script: Script): any {
if (this.dom) {
return this.dom.runVMScript(script);
}
return null;
}
}

module.exports = JSDomEnvironment;
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "./lib",
"skipLibCheck": true,
"strict": true
},
"include": ["src/**/*"],
"exclude": ["src/**/__mocks__/*", "src/**/__tests__/*"]
}

0 comments on commit 9069f61

Please sign in to comment.