Support promise return in ospec

This commit is contained in:
Stephan Hoyer 2017-08-04 09:23:20 +02:00 committed by Pierre-Yves Gérardy
parent 1ce8357866
commit 9b5afff52e
3 changed files with 111 additions and 21 deletions

View file

@ -148,6 +148,22 @@ o("setTimeout calls callback", function(done) {
})
```
Alternativly you can return a promise or even use an async function in tests:
```javascript
o("promise test", function() {
return new Promise(function(resolve) {
setTimeout(resolve, 10)
})
})
```
```javascript
o("promise test", async function() {
await someOtherAsyncFunction()
})
```
By default, asynchronous tests time out after 20ms. This can be changed on a per-test basis using the `timeout` argument:
```javascript
@ -158,7 +174,22 @@ o("setTimeout calls callback", function(done, timeout) {
})
```
Note that the `timeout` function call must be the first statement in its test.
Note that the `timeout` function call must be the first statement in its test. This currently does not work for promise tests. You can combine both methods to do this:
```javascript
o("promise test", function(done, timeout) {
timeout(1000)
someOtherAsyncFunctionThatTakes900ms().then(done)
})
```
```javascript
o("promise test", async function(done, timeout) {
timeout(1000)
await someOtherAsyncFunctionThatTakes900ms()
done()
})
```
Asynchronous tests generate an assertion that succeeds upon calling `done` or fails on timeout with the error message `async test timed out`.