In all the testing frameworks I have used, there is an optional parameter to specify you own custom error message.
This can be very useful, and I can't find a way to do this out of the box with jasmine.
I've had 3 other developers ask me about this exact functionality, and when it comes to jasmine I don't know what to tell them.
Is it possible to specify your own custom error message on each assertion ?
Custom Matchers describe('This custom matcher example', function() { beforeEach(function() { // We should add custom matched in beforeEach() function. jasmine. addMatchers ({ validateAge: function() { Return { compare: function(actual,expected) { var result = {}; result. pass = (actual > = 13 && actual < = 19); result.
It checks whether something is matched for a regular expression. You can use the toMatch matcher to test search patterns.
Jasmine already supports optional parameter in all matchers (toBe, toContain, and others), so you can use:
expect(true).toBe(false, 'True should be false').
Then in output it will look like this:
Message: Expected true to be false, 'True should be false'.
Link to commit (this is not described in documentation): https://github.com/ronanamsterdam/DefinitelyTyped/commit/ff104ed7cc13a3eb2e89f46242c4dbdbbe66665e
If you take a look at the jasmine source code you will see that there is no way to set the message from outside a matcher. For example the toBeNaN
matcher.
/** * Matcher that compares the actual to NaN. */ jasmine.Matchers.prototype.toBeNaN = function() { this.message = function() { return [ "Expected " + jasmine.pp(this.actual) + " to be NaN." ]; }; return (this.actual !== this.actual); };
As you can see the messages is hard coded into the matcher and will be set when you call the matcher. The only way I can think off to have your own messages is to write your matcher like described here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With