[ospec] Add o.timeout() to set a timeout for Promise-based async tests
This commit is contained in:
parent
b292f75bd8
commit
64680425e7
1 changed files with 13 additions and 3 deletions
|
|
@ -6,7 +6,7 @@ else window.o = m()
|
||||||
})(function init(name) {
|
})(function init(name) {
|
||||||
var spec = {}, subjects = [], results, only = null, ctx = spec, start, stack = 0, nextTickish, hasProcess = typeof process === "object", hasOwn = ({}).hasOwnProperty
|
var spec = {}, subjects = [], results, only = null, ctx = spec, start, stack = 0, nextTickish, hasProcess = typeof process === "object", hasOwn = ({}).hasOwnProperty
|
||||||
var ospecFileName = getStackName(ensureStackTrace(new Error), /[\/\\](.*?):\d+:\d+/), timeoutStackName
|
var ospecFileName = getStackName(ensureStackTrace(new Error), /[\/\\](.*?):\d+:\d+/), timeoutStackName
|
||||||
|
var globalTimeout = noTimeoutRightNow
|
||||||
if (name != null) spec[name] = ctx = {}
|
if (name != null) spec[name] = ctx = {}
|
||||||
|
|
||||||
function o(subject, predicate) {
|
function o(subject, predicate) {
|
||||||
|
|
@ -74,6 +74,9 @@ else window.o = m()
|
||||||
// now we're in user code (or past the stack end)
|
// now we're in user code (or past the stack end)
|
||||||
return stack[i]
|
return stack[i]
|
||||||
}
|
}
|
||||||
|
o.timeout = function(n) {
|
||||||
|
globalTimeout(n)
|
||||||
|
}
|
||||||
o.run = function(reporter) {
|
o.run = function(reporter) {
|
||||||
results = []
|
results = []
|
||||||
start = new Date
|
start = new Date
|
||||||
|
|
@ -123,6 +126,8 @@ else window.o = m()
|
||||||
var timeout = 0, delay = 200, s = new Date
|
var timeout = 0, delay = 200, s = new Date
|
||||||
var arg
|
var arg
|
||||||
|
|
||||||
|
globalTimeout = setDelay
|
||||||
|
|
||||||
var isDone = false
|
var isDone = false
|
||||||
// public API, may only be called once from use code (or after returned Promise resolution)
|
// public API, may only be called once from use code (or after returned Promise resolution)
|
||||||
function done(err) {
|
function done(err) {
|
||||||
|
|
@ -141,7 +146,6 @@ else window.o = m()
|
||||||
if (timeout !== undefined) timeout = clearTimeout(timeout)
|
if (timeout !== undefined) timeout = clearTimeout(timeout)
|
||||||
if (current === cursor) next()
|
if (current === cursor) next()
|
||||||
}
|
}
|
||||||
|
|
||||||
function startTimer() {
|
function startTimer() {
|
||||||
timeout = setTimeout(function() {
|
timeout = setTimeout(function() {
|
||||||
timeout = undefined
|
timeout = undefined
|
||||||
|
|
@ -149,12 +153,14 @@ else window.o = m()
|
||||||
}, Math.min(delay, 2147483647))
|
}, Math.min(delay, 2147483647))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setDelay (t) {delay = t}
|
||||||
|
|
||||||
if (fn.length > 0) {
|
if (fn.length > 0) {
|
||||||
var body = fn.toString()
|
var body = fn.toString()
|
||||||
arg = (body.match(/\(([\w$]+)/) || body.match(/([\w$]+)\s*=>/) || []).pop()
|
arg = (body.match(/\(([\w$]+)/) || body.match(/([\w$]+)\s*=>/) || []).pop()
|
||||||
if (body.indexOf(arg) === body.lastIndexOf(arg)) throw new Error("`" + arg + "()` should be called at least once")
|
if (body.indexOf(arg) === body.lastIndexOf(arg)) throw new Error("`" + arg + "()` should be called at least once")
|
||||||
try {
|
try {
|
||||||
fn(done, function(t) {delay = t})
|
fn(done, setDelay)
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
finalizeAsync(e)
|
finalizeAsync(e)
|
||||||
|
|
@ -171,6 +177,7 @@ else window.o = m()
|
||||||
nextTickish(next)
|
nextTickish(next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
globalTimeout = noTimeoutRightNow
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -261,6 +268,9 @@ else window.o = m()
|
||||||
else if (typeof value === "function") return value.name || "<anonymous function>"
|
else if (typeof value === "function") return value.name || "<anonymous function>"
|
||||||
try {return JSON.stringify(value)} catch (e) {return String(value)}
|
try {return JSON.stringify(value)} catch (e) {return String(value)}
|
||||||
}
|
}
|
||||||
|
function noTimeoutRightNow() {
|
||||||
|
throw new Error("o.timeout must be called snchronously from within a test definition or a hook")
|
||||||
|
}
|
||||||
var colorCodes = {
|
var colorCodes = {
|
||||||
red: "31m",
|
red: "31m",
|
||||||
red2: "31;1m",
|
red2: "31;1m",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue