Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80600 views
1
/**
2
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
3
*
4
* This source code is licensed under the BSD-style license found in the
5
* LICENSE file in the root directory of this source tree. An additional grant
6
* of patent rights can be found in the PATENTS file in the same directory.
7
*/
8
'use strict';
9
10
var FakeTimers = require('./lib/FakeTimers');
11
12
function _deepCopy(obj) {
13
var newObj = {};
14
var value;
15
for (var key in obj) {
16
value = obj[key];
17
if (typeof value === 'object' && value !== null) {
18
value = _deepCopy(value);
19
}
20
newObj[key] = value;
21
}
22
return newObj;
23
}
24
25
function JSDomEnvironment(config) {
26
// We lazily require jsdom because it takes a good ~.5s to load.
27
//
28
// Since this file may be require'd at the top of other files that may/may not
29
// use it (depending on the context -- such as TestRunner.js when operating as
30
// a workerpool parent), this is the best way to ensure we only spend time
31
// require()ing this when necessary.
32
this.global = require('./lib/jsdom-compat').jsdom().parentWindow;
33
34
// Node's error-message stack size is limited at 10, but it's pretty useful to
35
// see more than that when a test fails.
36
this.global.Error.stackTraceLimit = 100;
37
38
// Setup defaults for navigator.onLine
39
// TODO: It's questionable as to whether this should go here
40
// It's a pretty rarely depended on feature, so maybe tests that care
41
// about it should just shim it themselves?)
42
this.global.navigator.onLine = true;
43
44
// Pass through the node implementation of some needed APIs
45
this.global.ArrayBuffer = ArrayBuffer;
46
this.global.Float32Array = Float32Array;
47
this.global.Int16Array = Int16Array;
48
this.global.Int32Array = Int32Array;
49
this.global.Int8Array = Int8Array;
50
this.global.Uint8Array = Uint8Array;
51
this.global.Uint16Array = Uint16Array;
52
this.global.Uint32Array = Uint32Array;
53
this.global.DataView = DataView;
54
this.global.Buffer = Buffer;
55
this.global.process = process;
56
this.global.setImmediate = setImmediate;
57
this.global.clearImmediate = clearImmediate;
58
59
this.fakeTimers = new FakeTimers(this.global);
60
61
// I kinda wish tests just did this manually rather than relying on a
62
// helper function to do it, but I'm keeping it for backward compat reasons
63
// while we get jest deployed internally. Then we can look into removing it.
64
//
65
// #3376754
66
if (!this.global.hasOwnProperty('mockSetReadOnlyProperty')) {
67
this.global.mockSetReadOnlyProperty = function(obj, property, value) {
68
obj.__defineGetter__(property, function() {
69
return value;
70
});
71
};
72
}
73
74
// jsdom doesn't have support for window.Image, so we just replace it with a
75
// dummy constructor
76
try {
77
/* jshint nonew:false */
78
new this.global.Image();
79
} catch (e) {
80
this.global.Image = function Image() {};
81
}
82
83
// Pass through the node `process` global.
84
// TODO: Consider locking this down somehow so tests can't do crazy stuff to
85
// worker processes...
86
this.global.process = process;
87
88
// Apply any user-specified global vars
89
var globalValues = _deepCopy(config.globals);
90
for (var customGlobalKey in globalValues) {
91
// Always deep-copy objects so isolated test environments can't share memory
92
this.global[customGlobalKey] = globalValues[customGlobalKey];
93
}
94
}
95
96
JSDomEnvironment.prototype.dispose = function() {
97
// TODO: In node 0.8.8 (at least), closing each jsdom context appears to
98
// reproducibly cause a worker to segfault after a large number of tests
99
// have run (repro cases had > 1200).
100
//
101
// Disabling this to solve the problem for now -- but we should come
102
// back and investigate this soon. There's a reasonable chance that not
103
// closing out our contexts is eating an absurd amount of memory...
104
//this.global.close();
105
};
106
107
JSDomEnvironment.prototype.runSourceText = function(sourceText, fileName) {
108
return this.global.run(sourceText, fileName);
109
};
110
111
JSDomEnvironment.prototype.runWithRealTimers = function(cb) {
112
this.fakeTimers.runWithRealTimers(cb);
113
};
114
115
module.exports = JSDomEnvironment;
116
117