-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
188 lines (158 loc) · 4.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var extend = Object.assign || require('util')._extend // eslint-disable-line node/no-deprecated-api
var Path = require('path')
/*
* store original global keys
*/
var blacklist = Object.keys(global)
blacklist.push('constructor')
/*
* default config
*/
var defaults = {
url: 'http://localhost',
globalize: true,
console: true,
useEach: false,
skipWindowCheck: false,
html:
"<!doctype html><html><head><meta charset='utf-8'></head>" +
'<body></body></html>'
}
/*
* simple jsdom integration.
* You can pass jsdom options in, too:
*
* require('./support/jsdom')({
* src: [ jquery ]
* })
*/
module.exports = function (_options) {
var options = extend(extend({}, defaults), _options)
var keys = []
var before = options.useEach ? global.beforeEach : global.before
var after = options.useEach ? global.afterEach : global.after
/*
* register jsdom before the entire test suite
*/
before(function (next) {
if (global.window && !options.skipWindowCheck) {
throw new Error(
'mocha-jsdom: already a browser environment, or mocha-jsdom invoked ' +
"twice. use 'skipWindowCheck' to disable this check."
)
}
require('jsdom/lib/old-api').env(
extend(extend({}, options), { done: done })
)
function done (errors, window) {
if (options.globalize) {
propagateToGlobal(window)
} else {
global.window = window
}
if (options.console) {
window.console = global.console
}
if (errors) {
return next(getError(errors))
}
next(null)
}
})
/*
* undo keys from being propagated to global after the test suite
*/
after(function () {
if (options.globalize) {
keys.forEach(function (key) {
delete global[key]
})
} else {
delete global.window
}
})
/*
* propagate keys from `window` to `global`
*/
function propagateToGlobal (window) {
for (var key in window) {
if (!window.hasOwnProperty(key)) continue
if (~blacklist.indexOf(key)) continue
if (global[key]) {
if (process.env.JSDOM_VERBOSE) {
console.warn(
"[jsdom] Warning: skipping cleanup of global['" + key + "']"
)
}
continue
}
keys.push(key)
global[key] = window[key]
}
}
/*
* re-throws jsdom errors
*/
function getError (errors) {
var data = errors[0].data
var err = data.error
err.message = err.message + ' [jsdom]'
// clean up stack trace
if (err.stack) {
err.stack = err.stack
.split('\n')
.reduce(function (list, line) {
if (line.match(/node_modules.+(jsdom|mocha)/)) {
return list
}
line = line
.replace(/file:\/\/.*<script>/g, '<script>')
.replace(/:undefined:undefined/g, '')
list.push(line)
return list
}, [])
.join('\n')
}
return err
}
}
module.exports.rerequire = rerequire
/**
* Requires a module via `require()`, but invalidates the cache so it may be
* called again in the future. Useful for `mocha --watch`.
*
* var rerequire = require('mocha-jsdom').rerequire
* var $ = rerequire('jquery')
*/
function rerequire (module) {
if (module[0] === '.') {
module = Path.join(Path.dirname(getCaller()), module)
}
var oldkeys = Object.keys(require.cache)
var result = require(module)
var newkeys = Object.keys(require.cache)
newkeys.forEach(function (newkey) {
if (!~oldkeys.indexOf(newkey)) {
delete require.cache[newkey]
}
})
return result
}
/**
* Internal: gets the filename of the caller function. The `offset` defines how
* many hops away it's expected to be from in the stack trace.
*
* See: http://stackoverflow.com/questions/16697791/nodejs-get-filename-of-caller-function
*/
function getCaller (offset) {
/* eslint-disable handle-callback-err */
if (typeof offset !== 'number') offset = 1
var old = Error.prepareStackTrace
var err = new Error()
Error.prepareStackTrace = function (err, stack) {
return stack
}
var fname = err.stack[1 + offset].getFileName()
Error.prepareStackTrace = old
return fname
}