/
index.js
196 lines (166 loc) · 3.91 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
189
190
191
192
193
194
195
196
var sync = require('synchronize');
var fakeContext = {};
var context =
typeof global !== 'undefined' ? global :
typeof window !== 'undefined' ? window : fakeContext;
var has = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
var blockFunctionNames = [
'after',
'afterAll',
'afterEach',
'before',
'beforeAll',
'beforeEach',
'it',
'fit'
];
/**
* Wrap the given block function or hash of block functions with synchronize,
* and return the wrapped function(s).
*
* @param {Function|Object<String, Function>=} input
* @return {Function|Object}
*/
function wrap(input) {
var i, name, value, output;
if (arguments.length === 0 || input === undefined) {
checkContext();
input = {};
for (i = 0; i < blockFunctionNames.length; i++) {
name = blockFunctionNames[i];
if (typeof context[name] === 'function') {
input[name] = context[name];
}
}
}
if (typeof input === 'object' && input) {
output = {};
for (name in input) {
value = input[name];
if (typeof value === 'function') {
output[name] = wrapFn(value);
}
}
return output;
}
if (typeof input === 'function') {
return wrapFn(input);
}
throw new TypeError('expected an object or function to wrap');
}
var savedBlocks = null;
/**
* Overwrites the global block functions with their synchronized equivalents.
*/
function replace() {
var i, name, value;
checkContext();
if (savedBlocks) {
for (i = 0; i < blockFunctionNames.length; i++) {
name = blockFunctionNames[i];
value = context[name];
if (typeof value === 'function' && savedBlocks[name] !== value) {
savedBlocks[name] = value;
context[name] = wrapFn(value);
}
}
} else {
savedBlocks = {};
for (i = 0; i < blockFunctionNames.length; i++) {
name = blockFunctionNames[i];
value = context[name];
if (typeof value === 'function') {
savedBlocks[name] = value;
context[name] = wrapFn(value);
}
}
}
}
/**
* Undoes the global replacement from `syncIt.replace()`, and restores the
* original block functions.
*/
function restore() {
checkContext();
if (savedBlocks) {
for (var name in savedBlocks) {
context[name] = savedBlocks[name];
}
savedBlocks = null;
}
}
/**
* @private
*/
function wrapDone(done) {
// Jasmine support.
if (typeof done.fail === 'function') {
return function(err) {
if (err) done.fail(err);
else done();
};
}
return done;
}
/**
* @private
*/
function wrapFn(input) {
var output = function(name, fn) {
if (typeof name === 'function') {
return input(wrapBody(name));
}
return input(name, wrapBody(fn));
};
if (typeof input.only === 'function') {
output.only = function(name, fn) {
return input.only(name, wrapBody(fn));
};
}
if (typeof input.skip === 'function') {
output.skip = function(name) {
return input.skip(name);
};
}
return output;
}
/**
* @private
*/
function wrapBody(input) {
// Let the test runner handle a non-function.
if (typeof input !== 'function') return input;
// Don't double-wrap a function.
if (input.__syncit === fakeContext) return input;
var body = function(done) {
var self = this;
sync.fiber(function() {
input.call(self);
}, wrapDone(done));
};
body.__syncit = fakeContext;
return body;
}
/**
* @private
*/
function checkContext() {
if (context === fakeContext) {
throw new Error('unable to identify root scope');
}
}
exports.wrap = wrap;
exports.replace = replace;
exports.restore = restore;
blockFunctionNames.forEach(function(name) {
exports[name] = function(input) {
if (arguments.length === 0 || input === undefined) {
checkContext();
input = context[name];
}
if (typeof input === 'function') {
return wrapFn(input);
}
throw new TypeError('expected a function to wrap');
};
});