Skip to content

Commit 7170308

Browse files
committed
test.async -> test.cb
1 parent f13ba2e commit 7170308

File tree

9 files changed

+44
-40
lines changed

9 files changed

+44
-40
lines changed

lib/runner.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function Runner(opts) {
3333
serial: false,
3434
exclusive: false,
3535
skipped: false,
36-
async: false
36+
callback: false
3737
}, this._addFn.bind(this));
3838
}
3939

@@ -48,7 +48,7 @@ var chainableFunctions = {
4848
only: {exclusive: true},
4949
beforeEach: {type: 'beforeEach'},
5050
afterEach: {type: 'afterEach'},
51-
async: {async: true}
51+
cb: {callback: true}
5252
};
5353

5454
function makeChain(defaults, parentAdd) {

lib/test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ Test.prototype.run = function () {
142142
}
143143

144144
if (isPromise(ret)) {
145-
if (this.metadata.async) {
146-
self._setAssertError(new Error('Do not return ' + asyncType + ' from tests declared via `test.async(...)`, if you want to return a promise simply declare the test via `test(...)`'));
145+
if (this.metadata.callback) {
146+
self._setAssertError(new Error('Do not return ' + asyncType + ' from tests declared via `test.cb(...)`, if you want to return a promise simply declare the test via `test(...)`'));
147147
this.exit();
148148
return this.promise.promise;
149149
}
@@ -161,7 +161,7 @@ Test.prototype.run = function () {
161161

162162
self.exit();
163163
});
164-
} else if (!this.metadata.async) {
164+
} else if (!this.metadata.callback) {
165165
this.exit();
166166
}
167167

@@ -170,10 +170,10 @@ Test.prototype.run = function () {
170170

171171
Object.defineProperty(Test.prototype, 'end', {
172172
get: function () {
173-
if (this.metadata.async) {
173+
if (this.metadata.callback) {
174174
return this._end;
175175
}
176-
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)` ');
176+
throw new Error('t.end is not supported in this context. To use t.end as a callback, you must use "callback mode" via `test.cb(testName, fn)` ');
177177
}
178178
});
179179

readme.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Tests are run asynchronously and require you to return a supported async object
124124

125125
If you don't return one of the supported async objects mentioned above, the test is considered to be synchronous and ended immediately.
126126

127-
If you're unable to use promises or other supported async objects, you may enable legacy async support by defining your test with `test.async([title', fn)`. Tests declared this way **must** be manually ended with `t.end()`. This mode is mainly intended for testing callback-style APIs.
127+
If you're unable to use promises or other supported async objects, you may enable "callback mode" by defining your test with `test.cb([title', fn)`. Tests declared this way **must** be manually ended with `t.end()`. This mode is mainly intended for testing callback-style APIs.
128128

129129
You must define all tests synchronously. They can't be defined inside `setTimeout`, `setImmediate`, etc.
130130

@@ -173,7 +173,7 @@ test(t => {
173173
});
174174
});
175175

176-
test.async(t => {
176+
test.cb(t => {
177177
setTimeout(() => {
178178
t.pass();
179179
t.end();
@@ -199,10 +199,10 @@ test('auto ending is dangerous', t => {
199199
});
200200
```
201201

202-
For this to work, you must now use the legacy `async` test mode, and explicitly call `t.end()`.
202+
For this to work, you must now use "callback mode", and explicitly call `t.end()`.
203203

204204
```js
205-
test('explicitly end your tests', t => {
205+
test.cb('explicitly end your tests', t => {
206206
t.plan(2);
207207

208208
t.pass();
@@ -282,20 +282,24 @@ test(t => {
282282
});
283283
```
284284

285-
Both modern and legacy async support are available for hooks
285+
You may use async functions, return async objects, or enable "callback mode" in any of the hooks.
286286

287287
```js
288288
test.before(async t => {
289289
await promiseFn();
290290
});
291291

292-
test.async.beforeEach(t => {
292+
test.cb.beforeEach(t => {
293293
setTimeout(t.end);
294294
});
295295

296-
test.afterEach.async(t => {
296+
test.afterEach.cb(t => {
297297
setTimeout(t.end);
298298
});
299+
300+
test.after(t => {
301+
return new Promise(/* ... */);
302+
});
299303
```
300304

301305
The `beforeEach` & `afterEach` hooks can share context with the test:
@@ -433,7 +437,7 @@ test(async t => {
433437
AVA comes with builtin support for [observables](https://github.com/zenparsing/es-observable).
434438
If you return an observable from a test, AVA will automatically consume it to completion before ending the test.
435439

436-
*You don't have to use legacy `test.async` mode or `t.end()`.*
440+
*You do not need to use "callback mode" or call `t.end()`.*
437441

438442
```js
439443
test(t => {
@@ -449,10 +453,10 @@ test(t => {
449453

450454
### Callback support
451455

452-
AVA supports using `t.end` as the final callback when using node-style error-first callback APIs. AVA will consider any truthy value passed as the first argument to `t.end` to be an error. Note that `t.end` requires legacy `test.async` mode.
456+
AVA supports using `t.end` as the final callback when using node-style error-first callback APIs. AVA will consider any truthy value passed as the first argument to `t.end` to be an error. Note that `t.end` requires "callback mode", which can be enabled by using the `test.cb` chain.
453457

454458
```js
455-
test.async(t => {
459+
test.cb(t => {
456460
// t.end automatically checks for error as first argument
457461
fs.readFile('data.txt', t.end);
458462
});
@@ -463,7 +467,7 @@ test.async(t => {
463467

464468
### test([title], body)
465469
### test.serial([title], body)
466-
### test.async([title], body)
470+
### test.cb([title], body)
467471
### test.only([title], body)
468472
### test.skip([title], body)
469473
### test.before([title], body)
@@ -493,7 +497,7 @@ Plan how many assertion there are in the test. The test will fail if the actual
493497

494498
###### .end()
495499

496-
End the test. Only works with `test.async()`.
500+
End the test. Only works with `test.cb()`.
497501

498502

499503
## Assertions

test/fixture/long-running.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const test = require('../../');
33
var onExit = require('signal-exit');
44

5-
test.async('long running', function (t) {
5+
test.cb('long running', function (t) {
66
t.plan(1);
77

88
onExit(function () {

test/fixture/loud-rejection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const test = require('../../');
22

3-
test.async('creates an unhandled rejection', t => {
3+
test.cb('creates an unhandled rejection', t => {
44
Promise.reject(new Error(`You can't handle this!`));
55

66
setTimeout(function () {

test/fixture/serial.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ function randomDelay() {
66

77
const tests = [];
88

9-
test.async('first', t => {
9+
test.cb('first', t => {
1010
setTimeout(() => {
1111
tests.push('first');
1212
t.end();
1313
}, randomDelay());
1414
});
1515

16-
test.async('second', t => {
16+
test.cb('second', t => {
1717
setTimeout(() => {
1818
tests.push('second');
1919
t.end();

test/observable.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ var Observable = require('./fixture/observable');
55

66
function ava(fn) {
77
var a = _ava(fn);
8-
a.metadata = {async: false};
8+
a.metadata = {callback: false};
99
return a;
1010
}
1111

12-
ava.async = function (fn) {
12+
ava.cb = function (fn) {
1313
var a = _ava(fn);
14-
a.metadata = {async: true};
14+
a.metadata = {callback: true};
1515
return a;
1616
};
1717

1818
test('returning an observable from a legacy async fn is an error', function (t) {
19-
ava.async(function (a) {
19+
ava.cb(function (a) {
2020
a.plan(2);
2121

2222
var observable = Observable.of();

test/promise.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ var _ava = require('../lib/test');
55

66
function ava(fn) {
77
var a = _ava(fn);
8-
a.metadata = {async: false};
8+
a.metadata = {callback: false};
99
return a;
1010
}
1111

12-
ava.async = function (fn) {
12+
ava.cb = function (fn) {
1313
var a = _ava(fn);
14-
a.metadata = {async: true};
14+
a.metadata = {callback: true};
1515
return a;
1616
};
1717

@@ -30,7 +30,7 @@ function fail() {
3030
}
3131

3232
test('returning a promise from a legacy async fn is an error', function (t) {
33-
ava.async(function (a) {
33+
ava.cb(function (a) {
3434
a.plan(1);
3535

3636
return Promise.resolve(true).then(function () {

test/test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ var _ava = require('../lib/test');
44

55
function ava() {
66
var t = _ava.apply(null, arguments);
7-
t.metadata = {async: false};
7+
t.metadata = {callback: false};
88
return t;
99
}
1010

11-
ava.async = function () {
11+
ava.cb = function () {
1212
var t = _ava.apply(null, arguments);
13-
t.metadata = {async: true};
13+
t.metadata = {callback: true};
1414
return t;
1515
};
1616

@@ -101,7 +101,7 @@ test('handle non-assertion errors', function (t) {
101101
});
102102

103103
test('end can be used as callback without maintaining thisArg', function (t) {
104-
ava.async(function (a) {
104+
ava.cb(function (a) {
105105
setTimeout(a.end);
106106
}).run().then(function (a) {
107107
t.notOk(a.assertError);
@@ -226,7 +226,7 @@ test('run functions after last planned assertion', function (t) {
226226
test('run async functions after last planned assertion', function (t) {
227227
var i = 0;
228228

229-
ava.async(function (a) {
229+
ava.cb(function (a) {
230230
a.plan(1);
231231
a.pass();
232232
a.end();
@@ -238,7 +238,7 @@ test('run async functions after last planned assertion', function (t) {
238238
});
239239

240240
test('planned async assertion', function (t) {
241-
ava.async(function (a) {
241+
ava.cb(function (a) {
242242
a.plan(1);
243243

244244
setTimeout(function () {
@@ -252,7 +252,7 @@ test('planned async assertion', function (t) {
252252
});
253253

254254
test('async assertion with `.end()`', function (t) {
255-
ava.async(function (a) {
255+
ava.cb(function (a) {
256256
setTimeout(function () {
257257
a.pass();
258258
a.end();
@@ -276,7 +276,7 @@ test('more assertions than planned should emit an assertion error', function (t)
276276
});
277277

278278
test('record test duration', function (t) {
279-
ava.async(function (a) {
279+
ava.cb(function (a) {
280280
a.plan(1);
281281

282282
setTimeout(function () {
@@ -292,7 +292,7 @@ test('record test duration', function (t) {
292292
test('wait for test to end', function (t) {
293293
var avaTest;
294294

295-
ava.async(function (a) {
295+
ava.cb(function (a) {
296296
a.plan(1);
297297

298298
avaTest = a;

0 commit comments

Comments
 (0)