File tree Expand file tree Collapse file tree 3 files changed +46
-1
lines changed Expand file tree Collapse file tree 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) {
4040 if ( this . title === 'callee$0$0' ) {
4141 this . title = '[anonymous]' ;
4242 }
43+
44+ Object . keys ( Test . prototype ) . forEach ( function ( key ) {
45+ this [ key ] = this [ key ] . bind ( this ) ;
46+ } , this ) ;
4347}
4448
4549module . exports = Test ;
@@ -147,7 +151,17 @@ Test.prototype.run = function () {
147151 } . bind ( this ) ) ;
148152} ;
149153
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+
151165 if ( this . endCalled ) {
152166 throw new Error ( '.end() called more than once' ) ;
153167 }
Original file line number Diff line number Diff line change @@ -339,6 +339,19 @@ test(async t => {
339339
340340* You don't have to manually call ` t.end() ` .*
341341
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+
342355
343356## API
344357
Original file line number Diff line number Diff line change @@ -140,6 +140,24 @@ test('handle non-assertion errors', function (t) {
140140 } ) ;
141141} ) ;
142142
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+
143161test ( 'handle non-assertion errors even when planned' , function ( t ) {
144162 ava ( function ( a ) {
145163 a . plan ( 1 ) ;
You can’t perform that action at this time.
0 commit comments