Skip to content

Commit

Permalink
Merge pull request #281 from jamestalmage/async-to-cb
Browse files Browse the repository at this point in the history
Rename test.async to test.cb
  • Loading branch information
jamestalmage committed Nov 29, 2015
2 parents be946df + 7170308 commit 8cde259
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 40 deletions.
4 changes: 2 additions & 2 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function Runner(opts) {
serial: false,
exclusive: false,
skipped: false,
async: false
callback: false
}, this._addFn.bind(this));
}

Expand All @@ -48,7 +48,7 @@ var chainableFunctions = {
only: {exclusive: true},
beforeEach: {type: 'beforeEach'},
afterEach: {type: 'afterEach'},
async: {async: true}
cb: {callback: true}
};

function makeChain(defaults, parentAdd) {
Expand Down
10 changes: 5 additions & 5 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ Test.prototype.run = function () {
}

if (isPromise(ret)) {
if (this.metadata.async) {
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(...)`'));
if (this.metadata.callback) {
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(...)`'));
this.exit();
return this.promise.promise;
}
Expand All @@ -161,7 +161,7 @@ Test.prototype.run = function () {

self.exit();
});
} else if (!this.metadata.async) {
} else if (!this.metadata.callback) {
this.exit();
}

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

Object.defineProperty(Test.prototype, 'end', {
get: function () {
if (this.metadata.async) {
if (this.metadata.callback) {
return this._end;
}
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)` ');
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)` ');
}
});

Expand Down
28 changes: 16 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Tests are run asynchronously and require you to return a supported async object

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

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.
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.

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

Expand Down Expand Up @@ -173,7 +173,7 @@ test(t => {
});
});

test.async(t => {
test.cb(t => {
setTimeout(() => {
t.pass();
t.end();
Expand All @@ -199,10 +199,10 @@ test('auto ending is dangerous', t => {
});
```

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

```js
test('explicitly end your tests', t => {
test.cb('explicitly end your tests', t => {
t.plan(2);

t.pass();
Expand Down Expand Up @@ -282,20 +282,24 @@ test(t => {
});
```

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

```js
test.before(async t => {
await promiseFn();
});

test.async.beforeEach(t => {
test.cb.beforeEach(t => {
setTimeout(t.end);
});

test.afterEach.async(t => {
test.afterEach.cb(t => {
setTimeout(t.end);
});

test.after(t => {
return new Promise(/* ... */);
});
```

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

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

```js
test(t => {
Expand All @@ -449,10 +453,10 @@ test(t => {

### Callback support

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.
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.

```js
test.async(t => {
test.cb(t => {
// t.end automatically checks for error as first argument
fs.readFile('data.txt', t.end);
});
Expand All @@ -463,7 +467,7 @@ test.async(t => {

### test([title], body)
### test.serial([title], body)
### test.async([title], body)
### test.cb([title], body)
### test.only([title], body)
### test.skip([title], body)
### test.before([title], body)
Expand Down Expand Up @@ -493,7 +497,7 @@ Plan how many assertion there are in the test. The test will fail if the actual

###### .end()

End the test. Only works with `test.async()`.
End the test. Only works with `test.cb()`.


## Assertions
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/long-running.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const test = require('../../');
var onExit = require('signal-exit');

test.async('long running', function (t) {
test.cb('long running', function (t) {
t.plan(1);

onExit(function () {
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/loud-rejection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const test = require('../../');

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

setTimeout(function () {
Expand Down
4 changes: 2 additions & 2 deletions test/fixture/serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ function randomDelay() {

const tests = [];

test.async('first', t => {
test.cb('first', t => {
setTimeout(() => {
tests.push('first');
t.end();
}, randomDelay());
});

test.async('second', t => {
test.cb('second', t => {
setTimeout(() => {
tests.push('second');
t.end();
Expand Down
8 changes: 4 additions & 4 deletions test/observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ var Observable = require('./fixture/observable');

function ava(fn) {
var a = _ava(fn);
a.metadata = {async: false};
a.metadata = {callback: false};
return a;
}

ava.async = function (fn) {
ava.cb = function (fn) {
var a = _ava(fn);
a.metadata = {async: true};
a.metadata = {callback: true};
return a;
};

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

var observable = Observable.of();
Expand Down
8 changes: 4 additions & 4 deletions test/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ var _ava = require('../lib/test');

function ava(fn) {
var a = _ava(fn);
a.metadata = {async: false};
a.metadata = {callback: false};
return a;
}

ava.async = function (fn) {
ava.cb = function (fn) {
var a = _ava(fn);
a.metadata = {async: true};
a.metadata = {callback: true};
return a;
};

Expand All @@ -30,7 +30,7 @@ function fail() {
}

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

return Promise.resolve(true).then(function () {
Expand Down
18 changes: 9 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ var _ava = require('../lib/test');

function ava() {
var t = _ava.apply(null, arguments);
t.metadata = {async: false};
t.metadata = {callback: false};
return t;
}

ava.async = function () {
ava.cb = function () {
var t = _ava.apply(null, arguments);
t.metadata = {async: true};
t.metadata = {callback: true};
return t;
};

Expand Down Expand Up @@ -101,7 +101,7 @@ test('handle non-assertion errors', function (t) {
});

test('end can be used as callback without maintaining thisArg', function (t) {
ava.async(function (a) {
ava.cb(function (a) {
setTimeout(a.end);
}).run().then(function (a) {
t.notOk(a.assertError);
Expand Down Expand Up @@ -226,7 +226,7 @@ test('run functions after last planned assertion', function (t) {
test('run async functions after last planned assertion', function (t) {
var i = 0;

ava.async(function (a) {
ava.cb(function (a) {
a.plan(1);
a.pass();
a.end();
Expand All @@ -238,7 +238,7 @@ test('run async functions after last planned assertion', function (t) {
});

test('planned async assertion', function (t) {
ava.async(function (a) {
ava.cb(function (a) {
a.plan(1);

setTimeout(function () {
Expand All @@ -252,7 +252,7 @@ test('planned async assertion', function (t) {
});

test('async assertion with `.end()`', function (t) {
ava.async(function (a) {
ava.cb(function (a) {
setTimeout(function () {
a.pass();
a.end();
Expand All @@ -276,7 +276,7 @@ test('more assertions than planned should emit an assertion error', function (t)
});

test('record test duration', function (t) {
ava.async(function (a) {
ava.cb(function (a) {
a.plan(1);

setTimeout(function () {
Expand All @@ -292,7 +292,7 @@ test('record test duration', function (t) {
test('wait for test to end', function (t) {
var avaTest;

ava.async(function (a) {
ava.cb(function (a) {
a.plan(1);

avaTest = a;
Expand Down

0 comments on commit 8cde259

Please sign in to comment.