This repository has been archived by the owner on Jun 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
71 lines (63 loc) · 1.8 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
'use strict'
function waitsForPromise (fn, timeout) {
const promise = fn()
waitsFor('spec promise to resolve', function (done) {
promise.then(done, function (error) {
jasmine.getEnv().currentSpec.fail(error)
done()
})
}, timeout)
}
const names = ['beforeEach', 'afterEach', 'xit', 'it', 'fit', 'ffit', 'fffit']
const defaultOptions = {
timeout: 10 * 1000,
}
// Jasmine 1.3.x has no sane way of resetting to native clocks, and since we're
// gonna test promises and such, we're gonna need it
function resetClock() {
for (const key in jasmine.Clock.real) {
if (jasmine.Clock.real.hasOwnProperty(key)) {
window[key] = jasmine.Clock.real[key]
}
}
}
beforeEach(function() {
resetClock()
// Jasmine mocks Date.now()
// See steelbrain/jasmine-fix#6
jasmine.unspy(Date, 'now')
})
module.exports.patch = function patch(name) {
return function(arg1, arg2, arg3) {
const callback = typeof arg1 === 'function' ? arg1 : arg2
const description = typeof arg1 === 'string' ? arg1 : null
let optionsArg = {}
if (arg2 && typeof arg2 === 'object') {
optionsArg = arg2
} else if (arg3 && typeof arg3 === 'object') {
optionsArg = arg3
}
const options = Object.assign({}, defaultOptions, optionsArg)
const middleware = function() {
const value = callback()
if (value && typeof value.then === 'function') {
waitsForPromise(function() {
return value
}, options.timeout)
}
}
if (description) {
return global[name](description, middleware)
} else {
return global[name](middleware)
}
}
}
module.exports.wait = function(timeout) {
return new Promise(function(resolve) {
setTimeout(resolve, timeout)
})
}
names.forEach((name) => {
module.exports[name] = module.exports.patch(name)
})