Skip to content

Commit

Permalink
Add custom environment (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee authored Oct 2, 2017
1 parent cf20d5a commit d01b569
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 7 deletions.
1 change: 1 addition & 0 deletions jest-preset.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"^assets/(.*)": "<rootDir>/src/assets/$1",
"^environments/(.*)": "<rootDir>/src/environments/$1"
},
"testEnvironment": "jest-preset-angular/testEnvironment.js",
"transformIgnorePatterns": [
"node_modules/(?!@ngrx)"
]
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"license": "BSD-3",
"dependencies": {
"jest-zone-patch": "^0.0.7",
"jsdom": "^11.3.0",
"ts-jest": "^21.0.0"
},
"devDependencies": {
Expand Down
50 changes: 50 additions & 0 deletions testEnvironment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const {FakeTimers, installCommonGlobals} = require('jest-util');
const mock = require('jest-mock');

class JSDOMEnvironment {
constructor(config) {
// lazy require
const {JSDOM} = require('jsdom');

this.document = new JSDOM(undefined, {
url: config.testURL,
runScripts: 'dangerously'
});
const global = (this.global = this.document.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.
global.Error.stackTraceLimit = 100;
installCommonGlobals(global, config.globals);

this.moduleMocker = new mock.ModuleMocker(global);
this.fakeTimers = new FakeTimers(global, this.moduleMocker, config);
}

dispose() {
if (this.fakeTimers) {
this.fakeTimers.dispose();
}
if (this.global) {
this.global.close();
}
this.global = null;
this.document = null;
this.fakeTimers = null;
}

runScript(script) {
if (this.global) {
return this.document.runVMScript(script);
}
return null;
}
}

module.exports = JSDOMEnvironment;
Loading

0 comments on commit d01b569

Please sign in to comment.