如果深入研究,有一种方法可以获取行号:
How do you find out the caller function in JavaScript?
将您的断言包装在一个非常大的 try catch 中。然后打印出catch中的栈。
细节
在底部附加捕获。是这样的:
try {
// 300 asserts here
}catch (e) {
console.log(e);
console.log(`e.stack = ${e.stack}`);
}
当 PM 命中断言时,您将看到类似以下内容:
{type: "Error", name: "TypeError", message: "Cannot read property 'json' of undefined"}
e.stack = TypeError: Cannot read property 'json' of undefined
at Object.eval (eval at exec (evalmachine.<anonymous>:58:1931768), <anonymous>:89:30)
at u.exec (evalmachine.<anonymous>:58:1931803)
at t.exports (evalmachine.<anonymous>:58:5743)
at Object.<anonymous> (evalmachine.<anonymous>:58:7440)
at evalmachine.<anonymous>:15:26
at Array.forEach (<anonymous>)
at Object.emit (evalmachine.<anonymous>:14:54)
at evalmachine.<anonymous>:51:24
at evalmachine.<anonymous>:5:21
at evalmachine.<anonymous>:6:18
堆栈的顶部告诉您行号(从零开始):
在 Object.eval (eval at exec (evalmachine.:58:1931768), :89:30)
所以在这种情况下,89 在 IDE 中被报告为 88。
顺便说一句,看起来 30 实际上是被调用方法的列号。
额外的条目提供更长的跟踪,显示错误发生之前调用的沿袭,这在某些调试情况下可能很有用。
警告这将导致第一个断言错误中断任何进一步的测试。所以当你完成时撤消这个改变。