[ospec] o.defaultTimeout => o.specTimeout, and documentation.

The timeout parameter is deprecated but will still work for now
This commit is contained in:
Pierre-Yves Gérardy 2018-05-27 23:21:31 +02:00 committed by Pierre-Yves Gérardy
parent 25975c5ce4
commit aa0f9eae98
3 changed files with 50 additions and 24 deletions

View file

@ -164,30 +164,54 @@ o("promise test", async function() {
}) })
``` ```
By default, asynchronous tests time out after 20ms. This can be changed on a per-test basis using the `timeout` argument: #### Timeout delays
By default, asynchronous tests time out after 200ms. You can change that default for the current test suite and
its children by using the `o.specTimeout(delay)` function.
```javascript
o.spec("a spec that must timeout quickly", function(done, timeout) {
// wait 20ms before bailing out of the tests of this suite and
// its descendants
o.specTimeout(20)
o("some test", function(done) {
setTimeout(done, 10) // this will pass
})
o.spec("a child suite where the delay also applies", function () {
o("some test", function(done) {
setTimeout(done, 30) // this will time out.
})
})
})
o.spec("a spec that uses the default delay", function() {
// ...
})
```
This can also be changed on a per-test basis using the `o.timeout(delay)` function from within a test:
```javascript ```javascript
o("setTimeout calls callback", function(done, timeout) { o("setTimeout calls callback", function(done, timeout) {
timeout(50) //wait 50ms before bailing out of the test o.timeout(500) //wait 50ms before bailing out of the test
setTimeout(done, 30) setTimeout(done, 300)
}) })
``` ```
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: Note that the `o.timeout` function call must be the first statement in its test. It also works with Promise-returning tests:
```javascript ```javascript
o("promise test", function(done, timeout) { o("promise test", function() {
timeout(1000) o.timeout(1000)
someOtherAsyncFunctionThatTakes900ms().then(done) return someOtherAsyncFunctionThatTakes900ms()
}) })
``` ```
```javascript ```javascript
o("promise test", async function(done, timeout) { o("promise test", async function() {
timeout(1000) o.timeout(1000)
await someOtherAsyncFunctionThatTakes900ms() await someOtherAsyncFunctionThatTakes900ms()
done()
}) })
``` ```

View file

@ -25,11 +25,11 @@ else window.o = m()
o.after = hook("\x01after") o.after = hook("\x01after")
o.beforeEach = hook("\x01beforeEach") o.beforeEach = hook("\x01beforeEach")
o.afterEach = hook("\x01afterEach") o.afterEach = hook("\x01afterEach")
o.defaultTimeout = function (t) { o.specTimeout = function (t) {
if (isRunning()) throw new Error("o.defaultTimeout() can only be called before o.run()") if (isRunning()) throw new Error("o.specTimeout() can only be called before o.run()")
if (hasOwn.call(ctx, "\x01defaultTimeout")) throw new Error("A default timeout has already been defined in this context") if (hasOwn.call(ctx, "\x01specTimeout")) throw new Error("A default timeout has already been defined in this context")
if (typeof t !== "number") throw new Error("o.defaultTimeout() expects a number as argument") if (typeof t !== "number") throw new Error("o.specTimeout() expects a number as argument")
ctx["\x01defaultTimeout"] = t ctx["\x01specTimeout"] = t
} }
o.new = init o.new = init
o.spec = function(subject, predicate) { o.spec = function(subject, predicate) {
@ -97,7 +97,7 @@ else window.o = m()
}, null), 200 /*default timeout delay*/) }, null), 200 /*default timeout delay*/)
function test(spec, pre, post, finalize, defaultDelay) { function test(spec, pre, post, finalize, defaultDelay) {
if (hasOwn.call(spec, "\x01defaultTimeout")) defaultDelay = spec["\x01defaultTimeout"] if (hasOwn.call(spec, "\x01specTimeout")) defaultDelay = spec["\x01specTimeout"]
pre = [].concat(pre, spec["\x01beforeEach"] || []) pre = [].concat(pre, spec["\x01beforeEach"] || [])
post = [].concat(spec["\x01afterEach"] || [], post) post = [].concat(spec["\x01afterEach"] || [], post)
series([].concat(spec["\x01before"] || [], Object.keys(spec).reduce(function(tasks, key) { series([].concat(spec["\x01before"] || [], Object.keys(spec).reduce(function(tasks, key) {

View file

@ -360,12 +360,12 @@ o.spec("ospec", function() {
}) })
}) })
}) })
o.spec("o.defaultTimeout", function() { o.spec("o.specTimeout", function() {
o("throws when called inside of test definitions", function(done) { o("throws when called inside of test definitions", function(done) {
var err var err
var oo = o.new() var oo = o.new()
oo("", function() { oo("", function() {
try { oo.defaultTimeout(5) } catch (e) {err = e} try { oo.specTimeout(5) } catch (e) {err = e}
return {then: function(f) {setTimeout(f)}} return {then: function(f) {setTimeout(f)}}
}) })
oo.run(function(){ oo.run(function(){
@ -378,7 +378,7 @@ o.spec("ospec", function() {
var oo = o.new() var oo = o.new()
var t var t
oo.defaultTimeout(10) oo.specTimeout(10)
oo.beforeEach(function () { oo.beforeEach(function () {
t = new Date t = new Date
}) })
@ -405,7 +405,7 @@ o.spec("ospec", function() {
var oo = o.new() var oo = o.new()
var t var t
oo.defaultTimeout(10) oo.specTimeout(10)
oo.beforeEach(function () { oo.beforeEach(function () {
t = new Date t = new Date
}) })
@ -418,7 +418,7 @@ o.spec("ospec", function() {
oo.spec("nested 1", function () { oo.spec("nested 1", function () {
var t var t
oo.defaultTimeout(30) oo.specTimeout(30)
oo.beforeEach(function () { oo.beforeEach(function () {
t = new Date t = new Date
}) })
@ -445,7 +445,7 @@ o.spec("ospec", function() {
oo.spec("deeply", function() { oo.spec("deeply", function() {
var t var t
oo.defaultTimeout(20) oo.specTimeout(20)
oo.beforeEach(function () { oo.beforeEach(function () {
t = new Date t = new Date
}) })
@ -489,7 +489,8 @@ o.spec("ospec", function() {
o(err.message).equals("`oodone()` should only be called once") o(err.message).equals("`oodone()` should only be called once")
}) })
oo.run(function(results) { oo.run(function(results) {
o(results.length).equals(0) o(results.length).equals(1)
o(results[0].pass).equals(true)
done() done()
}) })
}) })
@ -507,7 +508,8 @@ o.spec("ospec", function() {
o(err.message).equals("`oodone()` should only be called once") o(err.message).equals("`oodone()` should only be called once")
}) })
oo.run(function(results) { oo.run(function(results) {
o(results.length).equals(0) o(results.length).equals(1)
o(results[0].pass).equals(true)
done() done()
}) })
}) })