File tree 3 files changed +46
-1
lines changed
3 files changed +46
-1
lines changed Original file line number Diff line number Diff line change @@ -40,6 +40,10 @@ function Test(title, fn) {
40
40
if ( this . title === 'callee$0$0' ) {
41
41
this . title = '[anonymous]' ;
42
42
}
43
+
44
+ Object . keys ( Test . prototype ) . forEach ( function ( key ) {
45
+ this [ key ] = this [ key ] . bind ( this ) ;
46
+ } , this ) ;
43
47
}
44
48
45
49
module . exports = Test ;
@@ -147,7 +151,17 @@ Test.prototype.run = function () {
147
151
} . bind ( this ) ) ;
148
152
} ;
149
153
150
- Test . prototype . end = function ( ) {
154
+ Test . prototype . end = function ( err ) {
155
+ if ( err ) {
156
+ this . assertError = new assert . AssertionError ( {
157
+ actual : err ,
158
+ message : 'Callback called with an error → ' + err ,
159
+ operator : 'callback'
160
+ } ) ;
161
+
162
+ return this . exit ( ) ;
163
+ }
164
+
151
165
if ( this . endCalled ) {
152
166
throw new Error ( '.end() called more than once' ) ;
153
167
}
Original file line number Diff line number Diff line change @@ -339,6 +339,19 @@ test(async t => {
339
339
340
340
* You don't have to manually call ` t.end() ` .*
341
341
342
+ ### Callback support
343
+
344
+ AVA supports using ` t.end ` as the final callback when using node-style
345
+ error-first callback APIs. AVA will consider any truthy value passed as
346
+ the first argument to ` t.end ` to be an error.
347
+
348
+ ``` js
349
+ test (t => {
350
+ // t.end automatically checks for error as first argument
351
+ fs .readFile (' data.txt' , t .end );
352
+ });
353
+ ```
354
+
342
355
343
356
## API
344
357
Original file line number Diff line number Diff line change @@ -140,6 +140,24 @@ test('handle non-assertion errors', function (t) {
140
140
} ) ;
141
141
} ) ;
142
142
143
+ test ( 'end can be used as callback without maintaining thisArg' , function ( t ) {
144
+ ava ( function ( a ) {
145
+ setTimeout ( a . end ) ;
146
+ } ) . run ( ) . then ( function ( a ) {
147
+ t . false ( a . assertError ) ;
148
+ t . end ( ) ;
149
+ } ) ;
150
+ } ) ;
151
+
152
+ test ( 'end can be used as callback with error' , function ( t ) {
153
+ ava ( function ( a ) {
154
+ a . end ( new Error ( 'failed' ) ) ;
155
+ } ) . run ( ) . catch ( function ( err ) {
156
+ t . true ( err instanceof Error ) ;
157
+ t . end ( ) ;
158
+ } ) ;
159
+ } ) ;
160
+
143
161
test ( 'handle non-assertion errors even when planned' , function ( t ) {
144
162
ava ( function ( a ) {
145
163
a . plan ( 1 ) ;
You can’t perform that action at this time.
0 commit comments