From faf0b97253b9733b25850f4e72d303803f8b86e6 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Wed, 21 Sep 2016 16:32:50 +0100 Subject: [PATCH 1/6] Use tabs in pitfalls code blocks --- docs/common-pitfalls.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/common-pitfalls.md b/docs/common-pitfalls.md index 0089fa052..8675882b8 100644 --- a/docs/common-pitfalls.md +++ b/docs/common-pitfalls.md @@ -24,9 +24,9 @@ You may be running an async operation inside a test and wondering why it's not f ```js test(t => { - return fetch().then(data => { - t.is(data, 'foo'); - }); + return fetch().then(data => { + t.is(data, 'foo'); + }); }); ``` @@ -34,10 +34,10 @@ If it uses callbacks, use [`test.cb`](https://github.com/avajs/ava#callback-supp ```js test.cb(t => { - fetch((err, data) => { - t.is(data, 'bar'); - t.end(); - }); + fetch((err, data) => { + t.is(data, 'bar'); + t.end(); + }); }); ``` From 6118ac387a675ba5f70479508573168dcde015fa Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Wed, 21 Sep 2016 16:38:38 +0100 Subject: [PATCH 2/6] Spell out async in pitfals doc --- docs/common-pitfalls.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/common-pitfalls.md b/docs/common-pitfalls.md index 8675882b8..1dcd8afc2 100644 --- a/docs/common-pitfalls.md +++ b/docs/common-pitfalls.md @@ -18,9 +18,9 @@ You may be using a service that only allows a limited number of concurrent conne Use the `concurrency` flag to limit the number of processes ran. For example, if your service plan allows 5 clients, you should run AVA with `concurrency=5` or less. -## Async operations +## Asynchronous operations -You may be running an async operation inside a test and wondering why it's not finishing. If your async operation uses promises, you should return the promise: +You may be running an asynchronous operation inside a test and wondering why it's not finishing. If your asynchronous operation uses promises, you should return the promise: ```js test(t => { From 85ef2cb00e9c9388462554008aeb6739efc45443 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Wed, 21 Sep 2016 16:39:04 +0100 Subject: [PATCH 3/6] Promote async/await in pitfalls doc --- docs/common-pitfalls.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/common-pitfalls.md b/docs/common-pitfalls.md index 1dcd8afc2..ffb0a9170 100644 --- a/docs/common-pitfalls.md +++ b/docs/common-pitfalls.md @@ -30,7 +30,16 @@ test(t => { }); ``` -If it uses callbacks, use [`test.cb`](https://github.com/avajs/ava#callback-support): +Better yet, use `async` / `await`: + +```js +test(async t => { + const data = await fetch(); + t.is(data, 'foo'); +}); +``` + +If you're using callbacks, use [`test.cb`](https://github.com/avajs/ava#callback-support): ```js test.cb(t => { From 8501a169e48d0f4c5625d892eb6002c00a620314 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Wed, 21 Sep 2016 16:39:51 +0100 Subject: [PATCH 4/6] Provide example of using pify --- docs/common-pitfalls.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/common-pitfalls.md b/docs/common-pitfalls.md index ffb0a9170..6caacb268 100644 --- a/docs/common-pitfalls.md +++ b/docs/common-pitfalls.md @@ -44,13 +44,20 @@ If you're using callbacks, use [`test.cb`](https://github.com/avajs/ava#callback ```js test.cb(t => { fetch((err, data) => { - t.is(data, 'bar'); + t.is(data, 'foo'); t.end(); }); }); ``` -Alternatively, promisify the callback function using something like [pify](https://github.com/sindresorhus/pify). +Alternatively, promisify the callback function using something like [`pify`](https://github.com/sindresorhus/pify): + +```js +test(async t => { + const data = await pify(fetch)(); + t.is(data, 'foo'); +}); +``` --- From 69c91cff3ed1acc8ba8afbb59e7804d820d3d3d1 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Wed, 21 Sep 2016 16:40:15 +0100 Subject: [PATCH 5/6] Suggest pify for catching exceptions --- docs/common-pitfalls.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/common-pitfalls.md b/docs/common-pitfalls.md index 6caacb268..c69428b63 100644 --- a/docs/common-pitfalls.md +++ b/docs/common-pitfalls.md @@ -59,6 +59,10 @@ test(async t => { }); ``` +### Attributing uncaught exceptions to tests + +AVA [can't trace uncaught exceptions](https://github.com/avajs/ava/issues/214) back to the test that triggered them. When working with asynchronous operations that take callbacks, consider promisifying and using `async`/`await`, as in the above example. + --- Is your problem not listed here? Submit a pull request or comment on [this issue](https://github.com/avajs/ava/issues/404). From 4441c7a06467c8b0c547693e3b90132cc27e4a2d Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Wed, 21 Sep 2016 16:46:24 +0100 Subject: [PATCH 6/6] Suggest promisifying callback-functions for better exception catching --- docs/common-pitfalls.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/common-pitfalls.md b/docs/common-pitfalls.md index c69428b63..b467aa410 100644 --- a/docs/common-pitfalls.md +++ b/docs/common-pitfalls.md @@ -61,7 +61,7 @@ test(async t => { ### Attributing uncaught exceptions to tests -AVA [can't trace uncaught exceptions](https://github.com/avajs/ava/issues/214) back to the test that triggered them. When working with asynchronous operations that take callbacks, consider promisifying and using `async`/`await`, as in the above example. +AVA [can't trace uncaught exceptions](https://github.com/avajs/ava/issues/214) back to the test that triggered them. Callback-taking functions may lead to uncaught exceptions that can then be hard to debug. Consider promisifying and using `async`/`await`, as in the above example. This should allow AVA to catch the exception and attribute it to the correct test. ---