Skip to content

Commit 615789a

Browse files
cpojerfacebook-github-bot-8
authored andcommitted
Clean up JSDomEnvironment
Summary: This cleans up the JSDomEnvironment and makes it actually very simple – it should make it more obvious and easier to create different environments (soon!). I'm also considering to make the `real-timer` use case a separate environment instead of a jest toggle (`useFakeTimers` and `useRealTimers`). This diff * Removes `mockSetReadOnlyProperty`, the API for this kind of stuff is `Object.defineProperty` which is almost the same * Removes setting `navigator.onLine`. Only a single test depended on this and it has a setter to set this property, so that was an easy fix. This shouldn't be mandated by the jsdom environment and should be a user specific setting. * Removes the script loading mechanism that we never actually used and doesn't work for us. I'm happy with `runInContext` for now, we can change it to something else in the future if we care to. * Doesn't check for `setImmediate` as its part of the node environment that we support public Reviewed By: voideanvalue Differential Revision: D2603413 fb-gh-sync-id: 085da5bf58e9f7b5645ce7772e73dcaafaff1fae
1 parent efa37b9 commit 615789a

File tree

2 files changed

+14
-80
lines changed

2 files changed

+14
-80
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* Example: http://astexplorer.net/#/zrybZ6UvRA
77
* Codemod: https://github.com/cpojer/js-codemod/blob/master/transforms/jest-update.js
88
* jscodeshift: https://github.com/facebook/jscodeshift
9+
* Removed `navigator.onLine` and `mockSetReadOnlyProperty` from the global jsdom
10+
environment. Use `window.navigator.onLine = true;` in your test setup and
11+
`Object.defineProperty` instead.
912

1013
## 0.6.1
1114

src/JSDomEnvironment.js

Lines changed: 11 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,14 @@
99

1010
const FakeTimers = require('./lib/FakeTimers');
1111
const utils = require('./lib/utils');
12-
13-
const USE_JSDOM_EVAL = false;
12+
const vm = require('vm');
1413

1514
class JSDomEnvironment {
1615

1716
constructor(config) {
18-
// We lazily require jsdom because it takes a good ~.5s to load.
19-
//
20-
// Since this file may be require'd at the top of other files that may/may
21-
// not use it (depending on the context -- such as TestRunner.js when
22-
// operating as a workerpool parent), this is the best way to ensure we
23-
// only spend time require()ing this when necessary.
24-
const jsdom = require('jsdom');
25-
this.document = jsdom.jsdom(/* markup */undefined, {
17+
// lazy require
18+
this.document = require('jsdom').jsdom(/* markup */undefined, {
2619
url: config.testURL,
27-
resourceLoader: this._fetchExternalResource.bind(this),
28-
features: {
29-
FetchExternalResources: ['script'],
30-
ProcessExternalResources: ['script'],
31-
},
3220
});
3321
this.global = this.document.defaultView;
3422

@@ -39,80 +27,23 @@ class JSDomEnvironment {
3927
// Forward some APIs
4028
this.global.Buffer = Buffer;
4129
this.global.process = process;
42-
43-
// Setup defaults for navigator.onLine
44-
this.global.navigator.onLine = true;
45-
46-
if (typeof setImmediate === 'function') {
47-
this.global.setImmediate = setImmediate;
48-
this.global.clearImmediate = clearImmediate;
49-
}
30+
this.global.setImmediate = setImmediate;
31+
this.global.clearImmediate = clearImmediate;
5032

5133
this.fakeTimers = new FakeTimers(this.global);
5234

53-
// I kinda wish tests just did this manually rather than relying on a
54-
// helper function to do it, but I'm keeping it for backward compat reasons
55-
// while we get jest deployed internally. Then we can look into removing it.
56-
//
57-
// #3376754
58-
if (!this.global.hasOwnProperty('mockSetReadOnlyProperty')) {
59-
this.global.mockSetReadOnlyProperty = function(obj, property, value) {
60-
obj.__defineGetter__(property, function() {
61-
return value;
62-
});
63-
};
64-
}
65-
66-
// Apply any user-specified global vars
67-
const globalValues = utils.deepCopy(config.globals);
68-
for (const customGlobalKey in globalValues) {
69-
// Always deep-copy objects so isolated test environments can't share
70-
// memory
71-
this.global[customGlobalKey] = globalValues[customGlobalKey];
72-
}
35+
Object.assign(this.global, utils.deepCopy(config.globals));
7336
}
7437

7538
dispose() {
7639
this.global.close();
7740
}
7841

79-
/**
80-
* Evaluates the given source text as if it were in a file with the given
81-
* name. This method returns nothing.
82-
*/
83-
runSourceText(sourceText, fileName) {
84-
if (!USE_JSDOM_EVAL) {
85-
const vm = require('vm');
86-
vm.runInContext(sourceText, this.document._ownerDocument._global, {
87-
filename: fileName,
88-
displayErrors: false,
89-
});
90-
return;
91-
}
92-
93-
// We evaluate code by inserting <script src="${filename}"> into the
94-
// document and using jsdom's resource loader to simulate serving the
95-
// source code.
96-
this._scriptToServe = sourceText;
97-
98-
const scriptElement = this.document.createElement('script');
99-
scriptElement.src = fileName;
100-
101-
this.document.head.appendChild(scriptElement);
102-
this.document.head.removeChild(scriptElement);
103-
}
104-
105-
_fetchExternalResource(
106-
resource,
107-
callback
108-
) {
109-
const content = this._scriptToServe;
110-
let error;
111-
delete this._scriptToServe;
112-
if (content === null || content === undefined) {
113-
error = new Error('Unable to find source for ' + resource.url.href);
114-
}
115-
callback(error, content);
42+
runSourceText(sourceText, filename) {
43+
vm.runInContext(sourceText, this.document._ownerDocument._global, {
44+
filename,
45+
displayErrors: false,
46+
});
11647
}
11748

11849
runWithRealTimers(cb) {

0 commit comments

Comments
 (0)