1

How can I make a test fail using javascript?

As per documentation this should do:

* if(true) throw 'custom message'

but I'm getting this error:

findAuthDataByUserAndApp.feature:20 - javascript evaluation failed: if(true) throw 'custom message', custom message in <eval> at line number 1 at column number 9

(What I would like to do is conditionally match response code based on __arg in a reusable feature like)

* if(__arg.statusCode != null && responseStatus != __arg.statusCode) throw 'custom message

Thanks

ivangsa
  • 117
  • 10

2 Answers2

1

I found an elegant solution for my original problem:

* def expectedStatusCode = req.statusCode || responseStatus
* match responseStatus == expectedStatusCode

(sometimes the solutions is just the simpler...)

ivangsa
  • 117
  • 10
0

You can't, and the throw is just a workaround for users who insist on some "extra" message in the log. The "error" is how it is designed currently, the test fails, it just "looks" like a JS error. We recommend other patterns.

* def failed = true
* if (failed) karate.log('custom message')
* assert !failed

If you insist, I can open a feature request for a karate.fail(condition, message) method that would do the above. IMHO it shouldn't be used, because tests should ideally be deterministic.

EDIT: karate.fail('message') will be available in 0.9.6 final

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • ok, but a little bit confusing because this exact example is in the documentation https://intuit.github.io/karate/#conditional-logic – ivangsa Apr 16 '20 at 14:03
  • @ivangsa ok, I can confirm we will fix and update the docs in the next release: https://github.com/intuit/karate/issues/1094#issuecomment-614681204 – Peter Thomas Apr 16 '20 at 14:17