This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9069f61
Showing
10 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
lib/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"trailingComma": "es5" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]>", | ||
"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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__/*"] | ||
} |