ospec: better stack trace filter, fix #2036

This commit is contained in:
Pierre-Yves Gérardy 2017-11-30 01:36:47 +01:00
parent a96e4d8aa6
commit 7caebddcb2
3 changed files with 18 additions and 10 deletions

View file

@ -490,7 +490,7 @@ True if the test passed. **No other keys will exist on the result if this value
### Error result.error
The value of the stack property from the `Error` object explaining the reason behind a failure.
The `Error` object explaining the reason behind a failure.
---

View file

@ -9,11 +9,7 @@ else window.o = m()
if (name != null) spec[name] = ctx = {}
try {throw new Error} catch (e) {
var stackTraceMatcher = new RegExp(
"^(?:(?!Error|"
+ e.stack.match(/[\/\\](.*?):\d+:\d+/)[1]
+ ").)*$", "gm"
)
var ospecFileName = e.stack.match(/[\/\\](.*?):\d+:\d+/)[1]
}
function o(subject, predicate) {
if (predicate === undefined) {
@ -58,8 +54,19 @@ else window.o = m()
spy.callCount = 0
return spy
}
o.cleanStackTrace = function(stack) {
return stack.match(stackTraceMatcher).pop()
o.cleanStackTrace = function(error) {
var i = 0, header = error.message ? error.name + ": " + error.message : error.name, stack
// some environments add the name and message to the stack trace
if (error.stack.indexOf(header) === 0) {
stack = error.stack.slice(header.length).split(/\r?\n/)
stack.shift() // drop the initial empty string
} else {
stack = error.stack.split(/\r?\n/)
}
// skip ospec-related entries on the stack
while (stack[i].indexOf(ospecFileName) !== -1) i++
// now we're in user code
return stack[i]
}
o.run = function(_reporter) {
results = []
@ -230,7 +237,8 @@ else window.o = m()
}
result.context = subjects.join(" > ")
result.message = message
result.error = error.stack
result.error = error
}
results.push(result)
}

View file

@ -188,7 +188,7 @@ o.spec("ospec", function() {
try {
throw new Error("line\nbreak")
} catch(error) {
var trace = o.cleanStackTrace(error.stack)
var trace = o.cleanStackTrace(error)
o(trace).notEquals("break")
o(trace.includes("test-ospec.js")).equals(true)
}