@@ -71,7 +71,9 @@ class UserCallback {
71
71
const TestMode = {
72
72
Run : 'run' ,
73
73
Skip : 'skip' ,
74
- Focus : 'focus'
74
+ Focus : 'focus' ,
75
+ Fail : 'fail' ,
76
+ Flake : 'flake'
75
77
} ;
76
78
77
79
const TestResult = {
@@ -276,6 +278,39 @@ class TestPass {
276
278
}
277
279
}
278
280
281
+ function specBuilder ( action ) {
282
+ let mode = TestMode . Run ;
283
+ let repeat = 1 ;
284
+
285
+ const func = ( ...args ) => {
286
+ for ( let i = 0 ; i < repeat ; ++ i )
287
+ action ( mode , ...args ) ;
288
+ mode = TestMode . Run ;
289
+ repeat = 1 ;
290
+ } ;
291
+
292
+ func . skip = condition => {
293
+ if ( condition )
294
+ mode = TestMode . Skip ;
295
+ return func ;
296
+ } ;
297
+ func . fail = condition => {
298
+ if ( condition )
299
+ mode = TestMode . Fail ;
300
+ return func ;
301
+ } ;
302
+ func . flake = condition => {
303
+ if ( condition )
304
+ mode = TestMode . Flake ;
305
+ return func ;
306
+ } ;
307
+ func . repeat = count => {
308
+ repeat = count ;
309
+ return func ;
310
+ } ;
311
+ return func ;
312
+ }
313
+
279
314
class TestRunner extends EventEmitter {
280
315
constructor ( options = { } ) {
281
316
super ( ) ;
@@ -303,41 +338,13 @@ class TestRunner extends EventEmitter {
303
338
}
304
339
}
305
340
306
- const duplicateTest = ( amount , mode , timeout ) => {
307
- return ( name , callback ) => {
308
- for ( let i = 0 ; i < amount ; ++ i )
309
- this . _addTest ( name , callback , mode , timeout ) ;
310
- }
311
- }
312
-
313
- const duplicateSuite = ( amount , mode ) => {
314
- return ( name , callback , ...args ) => {
315
- for ( let i = 0 ; i < amount ; ++ i )
316
- this . _addSuite ( mode , name , callback , ...args ) ;
317
- }
318
- }
319
-
320
- // bind methods so that they can be used as a DSL.
321
- this . describe = this . _addSuite . bind ( this , TestMode . Run ) ;
322
- this . describe . skip = condition => condition ? this . xdescribe : this . describe ;
323
- this . describe . repeat = number => duplicateSuite ( number , TestMode . Run ) ;
324
- this . fdescribe = this . _addSuite . bind ( this , TestMode . Focus ) ;
325
- this . fdescribe . skip = ( ) => this . fdescribe ; // no-op
326
- this . fdescribe . repeat = number => duplicateSuite ( number , TestMode . Focus ) ;
327
- this . xdescribe = this . _addSuite . bind ( this , TestMode . Skip ) ;
328
- this . xdescribe . skip = ( ) => this . xdescribe ; // no-op
329
- this . xdescribe . repeat = number => duplicateSuite ( number , TestMode . Skip ) ;
330
-
331
- this . it = ( name , callback ) => void this . _addTest ( name , callback , TestMode . Run , this . _timeout ) ;
332
- this . it . skip = condition => condition ? this . xit : this . it ;
333
- this . it . repeat = number => duplicateTest ( number , TestMode . Run , this . _timeout ) ;
334
- this . fit = ( name , callback ) => void this . _addTest ( name , callback , TestMode . Focus , this . _timeout ) ;
335
- this . fit . skip = ( ) => this . fit ; // no-op
336
- this . fit . repeat = number => duplicateTest ( number , TestMode . Focus , this . _timeout ) ;
337
- this . xit = ( name , callback ) => void this . _addTest ( name , callback , TestMode . Skip , this . _timeout ) ;
338
- this . xit . skip = ( ) => this . xit ; // no-op
339
- this . xit . repeat = number => duplicateTest ( number , TestMode . Skip , this . _timeout ) ;
340
-
341
+ this . describe = specBuilder ( ( mode , ...args ) => this . _addSuite ( mode , ...args ) ) ;
342
+ this . fdescribe = specBuilder ( ( mode , ...args ) => this . _addSuite ( TestMode . Focus , ...args ) ) ;
343
+ this . xdescribe = specBuilder ( ( mode , ...args ) => this . _addSuite ( TestMode . Skip , ...args ) ) ;
344
+ this . it = specBuilder ( ( mode , name , callback ) => this . _addTest ( name , callback , mode , this . _timeout ) ) ;
345
+ this . fit = specBuilder ( ( mode , name , callback ) => this . _addTest ( name , callback , TestMode . Focus , this . _timeout ) ) ;
346
+ this . xit = specBuilder ( ( mode , name , callback ) => this . _addTest ( name , callback , TestMode . Skip , this . _timeout ) ) ;
347
+
341
348
this . _debuggerLogBreakpointLines = new Multimap ( ) ;
342
349
this . dit = ( name , callback ) => {
343
350
const test = this . _addTest ( name , callback , TestMode . Focus , INFINITE_TIMEOUT ) ;
0 commit comments