66
66
* function maybe() { return Math.random() < 0.5; }
67
67
*/
68
68
69
+ 'use strict' ;
70
+
69
71
var promise = require ( '..' ) . promise ;
70
72
var flow = promise . controlFlow ( ) ;
71
73
@@ -196,6 +198,45 @@ function ignore(predicateFn) {
196
198
}
197
199
198
200
201
+ /**
202
+ * @param {string } name
203
+ * @return {!Function }
204
+ * @throws {TypeError }
205
+ */
206
+ function getMochaGlobal ( name ) {
207
+ let fn = global [ name ] ;
208
+ let type = typeof fn ;
209
+ if ( type !== 'function' ) {
210
+ throw TypeError (
211
+ `Expected global.${ name } to be a function, but is ${ type } . `
212
+ + 'This can happen if you try using this module when running '
213
+ + 'with node directly instead of using the mocha executable' ) ;
214
+ }
215
+ return fn ;
216
+ }
217
+
218
+
219
+ const WRAPPED = {
220
+ after : null ,
221
+ afterEach : null ,
222
+ before : null ,
223
+ beforeEach : null ,
224
+ it : null ,
225
+ itOnly : null ,
226
+ xit : null
227
+ } ;
228
+
229
+
230
+ function wrapIt ( ) {
231
+ if ( ! WRAPPED . it ) {
232
+ let it = getMochaGlobal ( 'it' ) ;
233
+ WRAPPED . it = wrapped ( it ) ;
234
+ WRAPPED . itOnly = wrapped ( it . only ) ;
235
+ }
236
+ }
237
+
238
+
239
+
199
240
// PUBLIC API
200
241
201
242
@@ -211,67 +252,134 @@ exports.controlFlow = function(){
211
252
/**
212
253
* Registers a new test suite.
213
254
* @param {string } name The suite name.
214
- * @param {function()= } fn The suite function, or {@code undefined} to define
255
+ * @param {function()= } opt_fn The suite function, or `undefined` to define
256
+ * a pending test suite.
257
+ */
258
+ exports . describe = function ( name , opt_fn ) {
259
+ let fn = getMochaGlobal ( 'describe' ) ;
260
+ return opt_fn ? fn ( name , opt_fn ) : fn ( name ) ;
261
+ } ;
262
+
263
+
264
+ /**
265
+ * Defines a suppressed test suite.
266
+ * @param {string } name The suite name.
267
+ * @param {function()= } opt_fn The suite function, or `undefined` to define
215
268
* a pending test suite.
216
269
*/
217
- exports . describe = global . describe ;
270
+ exports . describe . skip = function ( name , opt_fn ) {
271
+ let fn = getMochaGlobal ( 'describe' ) ;
272
+ return opt_fn ? fn . skip ( name , opt_fn ) : fn . skip ( name ) ;
273
+ } ;
274
+
218
275
219
276
/**
220
277
* Defines a suppressed test suite.
221
278
* @param {string } name The suite name.
222
- * @param {function()= } fn The suite function, or { @code undefined} to define
279
+ * @param {function()= } opt_fn The suite function, or ` undefined` to define
223
280
* a pending test suite.
224
281
*/
225
- exports . xdescribe = global . xdescribe ;
226
- exports . describe . skip = global . describe . skip ;
282
+ exports . xdescribe = function ( name , opt_fn ) {
283
+ let fn = getMochaGlobal ( 'xdescribe' ) ;
284
+ return opt_fn ? fn ( name , opt_fn ) : fn ( name ) ;
285
+ } ;
286
+
227
287
228
288
/**
229
289
* Register a function to call after the current suite finishes.
230
290
* @param {function() } fn .
231
291
*/
232
- exports . after = wrapped ( global . after ) ;
292
+ exports . after = function ( fn ) {
293
+ if ( ! WRAPPED . after ) {
294
+ WRAPPED . after = wrapped ( getMochaGlobal ( 'after' ) ) ;
295
+ }
296
+ WRAPPED . after ( fn ) ;
297
+ } ;
298
+
233
299
234
300
/**
235
301
* Register a function to call after each test in a suite.
236
302
* @param {function() } fn .
237
303
*/
238
- exports . afterEach = wrapped ( global . afterEach ) ;
304
+ exports . afterEach = function ( fn ) {
305
+ if ( ! WRAPPED . afterEach ) {
306
+ WRAPPED . afterEach = wrapped ( getMochaGlobal ( 'afterEach' ) ) ;
307
+ }
308
+ WRAPPED . afterEach ( fn ) ;
309
+ } ;
310
+
239
311
240
312
/**
241
313
* Register a function to call before the current suite starts.
242
314
* @param {function() } fn .
243
315
*/
244
- exports . before = wrapped ( global . before ) ;
316
+ exports . before = function ( fn ) {
317
+ if ( ! WRAPPED . before ) {
318
+ WRAPPED . before = wrapped ( getMochaGlobal ( 'before' ) ) ;
319
+ }
320
+ WRAPPED . before ( fn ) ;
321
+ } ;
245
322
246
323
/**
247
324
* Register a function to call before each test in a suite.
248
325
* @param {function() } fn .
249
326
*/
250
- exports . beforeEach = wrapped ( global . beforeEach ) ;
327
+ exports . beforeEach = function ( fn ) {
328
+ if ( ! WRAPPED . beforeEach ) {
329
+ WRAPPED . beforeEach = wrapped ( getMochaGlobal ( 'beforeEach' ) ) ;
330
+ }
331
+ WRAPPED . beforeEach ( fn ) ;
332
+ } ;
251
333
252
334
/**
253
335
* Add a test to the current suite.
254
336
* @param {string } name The test name.
255
- * @param {function()= } fn The test function, or { @code undefined} to define
337
+ * @param {function()= } opt_fn The test function, or ` undefined` to define
256
338
* a pending test case.
257
339
*/
258
- exports . it = wrapped ( global . it ) ;
340
+ exports . it = function ( name , opt_fn ) {
341
+ wrapIt ( ) ;
342
+ if ( opt_fn ) {
343
+ WRAPPED . it ( name , opt_fn ) ;
344
+ } else {
345
+ WRAPPED . it ( name ) ;
346
+ }
347
+ } ;
259
348
260
349
/**
261
350
* An alias for {@link #it()} that flags the test as the only one that should
262
351
* be run within the current suite.
263
352
* @param {string } name The test name.
264
- * @param {function()= } fn The test function, or { @code undefined} to define
353
+ * @param {function()= } opt_fn The test function, or ` undefined` to define
265
354
* a pending test case.
266
355
*/
267
- exports . iit = exports . it . only = wrapped ( global . it . only ) ;
356
+ exports . it . only = function ( name , opt_fn ) {
357
+ wrapIt ( ) ;
358
+ if ( opt_fn ) {
359
+ WRAPPED . itOnly ( name , opt_fn ) ;
360
+ } else {
361
+ WRAPPED . itOnly ( name ) ;
362
+ }
363
+ } ;
364
+
268
365
269
366
/**
270
367
* Adds a test to the current suite while suppressing it so it is not run.
271
368
* @param {string } name The test name.
272
- * @param {function()= } fn The test function, or { @code undefined} to define
369
+ * @param {function()= } opt_fn The test function, or ` undefined` to define
273
370
* a pending test case.
274
371
*/
275
- exports . xit = exports . it . skip = wrapped ( global . xit ) ;
372
+ exports . xit = function ( name , opt_fn ) {
373
+ if ( ! WRAPPED . xit ) {
374
+ WRAPPED . xit = wrapped ( getMochaGlobal ( 'xit' ) ) ;
375
+ }
376
+ if ( opt_fn ) {
377
+ WRAPPED . xit ( name , opt_fn ) ;
378
+ } else {
379
+ WRAPPED . xit ( name ) ;
380
+ }
381
+ } ;
382
+
276
383
384
+ exports . it . skip = exports . xit ;
277
385
exports . ignore = ignore ;
0 commit comments