0

So I m getting an assertion error in postman, and the error looks like this:

There was an error in evaluating the test script:  AssertionError: expected undefined to be a string

But i have 300 assertions for this API, and is virtually impossible to idenftify what assertion actually failed, is there a way for me to make postman show the line number that raised the assertionFail?

Ovr99
  • 11
  • 4
  • There isn't a way to address this just yet (open request: https://github.com/postmanlabs/postman-app-support/issues/3803). If you have 300 assertions in a single `pm.test()` I would say you need to start dividing those in smaller tests. The only solution I can offer is a binary search, with 300 assertions, you'll get there in 9 steps. Just comment/delete half the assertions, to check which half has the error, and keep halving until you only have one test. Hard work yes, but it's certain you'll find the test in 9 steps at most. – bitoiu Aug 26 '22 at 07:44

1 Answers1

1

There is a way to get the line number if you dig down into this: How do you find out the caller function in JavaScript?

Wrap your asserts in a very large try catch. Then print out the stack in the catch.

Details

Append the catch at the bottom. Something like this:

 try {
    // 300 asserts here


}catch (e) {
    console.log(e);
    console.log(`e.stack = ${e.stack}`);  
    throw e;
}

When PM hits the assert you will see something like:

{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
 

The top of the stack tells you the line number (zero-based):

at Object.eval (eval at exec (evalmachine.:58:1931768), :89:30)

So in this case 89, is reported as 88 in the IDE. BTW it appears that the 30 is actually the column number of the called method.

Additional entries give a longer trace, showing the lineage of the calls before the error occurred, which may be useful in some debugging cases.

Warning this will cause the first assert error to interrupt any further testing. So undo this change when you are done.

Andrew Dennison
  • 1,069
  • 11
  • 9
  • Good workaround! I use (nested) shared test functions and the stacktrace really helps me find out which function throws. One addition: You need to add `throw e` to the catch block so that Postman shows that `pm.test()` actually failed. – Martin Trenkmann Jan 19 '23 at 12:08
  • 1
    @MartinTrenkmann Good point. Eating/ignoring errors is always bad practice. – Andrew Dennison Mar 02 '23 at 05:42