@@ -28,6 +28,11 @@ function Test(title, fn) {
28
28
this . duration = null ;
29
29
this . context = { } ;
30
30
31
+ // TODO(jamestalmage): make this an optional constructor arg instead of having Runner set it after the fact.
32
+ // metadata should just always exist, otherwise it requires a bunch of ugly checks all over the place.
33
+ // I will fix this correctly in the upstream PR.
34
+ this . metadata = { } ;
35
+
31
36
// store the time point before test execution
32
37
// to calculate the total time spent in test
33
38
this . _timeStart = null ;
@@ -47,7 +52,9 @@ module.exports = Test;
47
52
Test . prototype . _assert = function ( ) {
48
53
this . assertCount ++ ;
49
54
50
- if ( this . assertCount === this . planCount ) {
55
+ // TODO(jamestalmage): no only auto-ends in "declared" async mode `test.async(...)`. Should we drop auto-end behavior all together?
56
+ // https://github.com/sindresorhus/ava/issues/244#issuecomment-158705839
57
+ if ( this . metadata . async && this . assertCount === this . planCount ) {
51
58
setImmediate ( this . exit . bind ( this ) ) ;
52
59
}
53
60
} ;
@@ -123,27 +130,38 @@ Test.prototype.run = function () {
123
130
ret = observableToPromise ( ret ) ;
124
131
125
132
if ( isPromise ( ret ) ) {
126
- ret
127
- . then ( function ( ) {
128
- if ( ! self . planCount || self . planCount === self . assertCount ) {
129
- self . exit ( ) ;
130
- }
131
- } )
132
- . catch ( function ( err ) {
133
- self . assertError = new assert . AssertionError ( {
134
- actual : err ,
135
- message : 'Promise rejected → ' + err ,
136
- operator : 'promise'
137
- } ) ;
138
-
133
+ if ( ! this . metadata . async ) {
134
+ ret = ret . then ( function ( ) {
139
135
self . exit ( ) ;
140
136
} ) ;
137
+ }
138
+
139
+ ret . catch ( function ( err ) {
140
+ self . assertError = new assert . AssertionError ( {
141
+ actual : err ,
142
+ message : 'Promise rejected → ' + err ,
143
+ operator : 'promise'
144
+ } ) ;
145
+
146
+ self . exit ( ) ;
147
+ } ) ;
148
+ } else if ( ! this . metadata . async ) {
149
+ this . exit ( ) ;
141
150
}
142
151
143
152
return this . promise . promise ;
144
153
} ;
145
154
146
- Test . prototype . end = function ( err ) {
155
+ Object . defineProperty ( Test . prototype , 'end' , {
156
+ get : function ( ) {
157
+ if ( this . metadata . async ) {
158
+ return this . _end ;
159
+ }
160
+ throw new Error ( 't.end is not supported in this context. To use t.end as a callback, you must explicitly declare the test asynchronous via `test.async(testName, fn)` ' ) ;
161
+ }
162
+ } ) ;
163
+
164
+ Test . prototype . _end = function ( err ) {
147
165
if ( err ) {
148
166
this . assertError = new assert . AssertionError ( {
149
167
actual : err ,
0 commit comments