@@ -4,88 +4,80 @@ const isPromise = require('is-promise');
4
4
const autoBind = require ( 'auto-bind' ) ;
5
5
const AvaError = require ( './ava-error' ) ;
6
6
7
- function noop ( ) { }
7
+ class Concurrent {
8
+ constructor ( tests , bail ) {
9
+ if ( ! Array . isArray ( tests ) ) {
10
+ throw new TypeError ( 'Expected an array of tests' ) ;
11
+ }
8
12
9
- module . exports = Concurrent ;
13
+ this . results = [ ] ;
14
+ this . passed = true ;
15
+ this . reason = null ;
16
+ this . tests = tests ;
17
+ this . bail = bail || false ;
10
18
11
- function Concurrent ( tests , bail ) {
12
- if ( ! ( this instanceof Concurrent ) ) {
13
- throw new TypeError ( 'Class constructor Concurrent cannot be invoked without \'new\'' ) ;
19
+ autoBind ( this ) ;
14
20
}
21
+ run ( ) {
22
+ let results ;
15
23
16
- if ( ! Array . isArray ( tests ) ) {
17
- throw new TypeError ( 'Expected an array of tests' ) ;
18
- }
19
-
20
- this . results = [ ] ;
21
- this . passed = true ;
22
- this . reason = null ;
23
- this . tests = tests ;
24
- this . bail = bail || false ;
25
-
26
- autoBind ( this ) ;
27
- }
28
-
29
- Concurrent . prototype . run = function ( ) {
30
- let results ;
24
+ try {
25
+ results = this . tests . map ( this . _runTest ) ;
26
+ } catch ( err ) {
27
+ if ( err instanceof AvaError ) {
28
+ return this . _results ( ) ;
29
+ }
31
30
32
- try {
33
- results = this . tests . map ( this . _runTest ) ;
34
- } catch ( err ) {
35
- if ( err instanceof AvaError ) {
36
- return this . _results ( ) ;
31
+ throw err ;
37
32
}
38
33
39
- throw err ;
40
- }
34
+ const isAsync = results . some ( isPromise ) ;
41
35
42
- const isAsync = results . some ( isPromise ) ;
36
+ if ( isAsync ) {
37
+ return Promise . all ( results )
38
+ . catch ( AvaError , ( ) => { } )
39
+ . then ( this . _results ) ;
40
+ }
43
41
44
- if ( isAsync ) {
45
- return Promise . all ( results )
46
- . catch ( AvaError , noop )
47
- . then ( this . _results ) ;
42
+ return this . _results ( ) ;
48
43
}
44
+ _runTest ( test , index ) {
45
+ const result = test . run ( ) ;
49
46
50
- return this . _results ( ) ;
51
- } ;
52
-
53
- Concurrent . prototype . _runTest = function ( test , index ) {
54
- const result = test . run ( ) ;
47
+ if ( isPromise ( result ) ) {
48
+ return result . then ( result => this . _addResult ( result , index ) ) ;
49
+ }
55
50
56
- if ( isPromise ( result ) ) {
57
- return result . then ( result => this . _addResult ( result , index ) ) ;
51
+ return this . _addResult ( result , index ) ;
58
52
}
53
+ _addResult ( result , index ) {
54
+ // always save result when not in bail mode or all previous tests pass
55
+ if ( ( this . bail && this . passed ) || ! this . bail ) {
56
+ this . results [ index ] = result ;
57
+ }
59
58
60
- return this . _addResult ( result , index ) ;
61
- } ;
59
+ if ( result . passed === false ) {
60
+ this . passed = false ;
62
61
63
- Concurrent . prototype . _addResult = function ( result , index ) {
64
- // always save result when not in bail mode or all previous tests pass
65
- if ( ( this . bail && this . passed ) || ! this . bail ) {
66
- this . results [ index ] = result ;
67
- }
62
+ // only set reason once
63
+ if ( ! this . reason ) {
64
+ this . reason = result . reason ;
65
+ }
68
66
69
- if ( result . passed === false ) {
70
- this . passed = false ;
71
-
72
- // only set reason once
73
- if ( ! this . reason ) {
74
- this . reason = result . reason ;
67
+ if ( this . bail ) {
68
+ throw new AvaError ( 'Error in Concurrent while in bail mode' ) ;
69
+ }
75
70
}
76
71
77
- if ( this . bail ) {
78
- throw new AvaError ( 'Error in Concurrent while in bail mode' ) ;
79
- }
72
+ return result ;
80
73
}
74
+ _results ( ) {
75
+ return {
76
+ passed : this . passed ,
77
+ reason : this . reason ,
78
+ result : this . results
79
+ } ;
80
+ }
81
+ }
81
82
82
- return result ;
83
- } ;
84
-
85
- Concurrent . prototype . _results = function ( ) {
86
- return {
87
- passed : this . passed ,
88
- reason : this . reason ,
89
- result : this . results
90
- } ;
91
- } ;
83
+ module . exports = Concurrent ;
0 commit comments