custom failure message on any jasmine v2 assertion
MIT License
works with
jasmine v2
(for work withjasmine v1.3
see jasmine-custom-message)
This script makes it possible to use your own failure message on any jasmine assertion.
describe('the story', function() {
it('should finish ok', function() {
since('all cats are grey in the dark').
expect('tiger').toEqual('kitty'); // => 'all cats are grey in the dark'
});
});
All the magic happens in since
function. That returns an object with a property expect
. That contains no more than a wrapped jasmine expect
function. That returns jasmine expectation
object with a wrapped addExpectationResult
function. That can replace an ordinary jasmine failure message with a newly generated one. That is generating based on a custom message you have supplied to since
function as the first argument. That can be a primitive (except null
and undefined
), a function, or any other object. That is it.
describe('test', function() {
it('should be ok', function() {
since(function() {
return {'tiger': 'kitty'};
}).
expect(3).toEqual(4); // => '{"tiger":"kitty"}'
});
});
You can use jasmine as you did before, since jasmine2-custom-message
does not replace global jasmine expect
function.
describe('test', function() {
it('should be ok', function() {
expect(3).toEqual(4); // => ordinary jasmine message
});
});
You can use expected and actual values of the assertion in your custom message by:
this.actual
and this.expected
#{actual}
and #{expected}
You can include the full original message from Jasmine by:
this.message
#{message}
describe('test', function() {
it('should be ok', function() {
since(function() {
return this.actual + ' =/= ' + this.expected;
}).
expect(3).toEqual(4); // => '3 =/= 4'
});
});
describe('multiple tests that need some context added to the message', function() {
it('should be ok for all options', function() {
// passes the 1st loop iteration, fails the 2nd
[1, 2, 3, 4, 5].forEach(testOptionIndex => {
since(function() {
return 'for test option ' + testOptionIndex + ': ' + this.message;
}).
expect(testOptionIndex).toEqual(1); // => for test option 2: Expected 2 to equal 1.
});
});
});
describe('test', function() {
it('should be ok', function() {
since('#{actual} =/= #{expected}').
expect(3).toEqual(4); // => '3 =/= 4'
});
});
describe('multiple tests that need some context added to the message', function() {
it('should be ok for all options', function() {
// passes the 1st loop iteration, fails the 2nd
[1, 2, 3, 4, 5].forEach(testOptionIndex => {
since('for test option ' + testOptionIndex + ': #{message}').
expect(testOptionIndex).toEqual(1); // => for test option 2: Expected 2 to equal 1.
});
});
});
bower install jasmine2-custom-message --save-dev
jasmine2-custom-message.js
into your HTML file next to jasmine
script<script src="PATH-TO/jasmine.js"></script>
<script src="PATH-TO/jasmine2-custom-message.js"></script>
$ bower install jasmine2-custom-message --save-dev
or
$ npm install jasmine2-custom-message --save-dev
require('jasmine2-custom-message');
var since = require('jasmine2-custom-message');
v0.9.0 - 2018.03.01
#{message}
added for the original jasmine2 error message (kudos to Keith Zimmerman)toHaveBeenCalled
matcher (kudos to Holger Jeromin)protractor
environment (kudos to Keith Zimmerman)typescript
definitions (kudos to Holger Jeromin and Andrew N Marshall)v0.8.0 - 2015.08.05
#{actual}
and #{expected}
protractor
environmentprotractor
v0.7.0 - 2014.10.23
v0.6.0
- 2014.01.18 - BROKEN COMPATIBILITY!
since
functionv0.5.0
- 2014.01.15
it
and expect
functions in browsersv0.2.0
- 2014.01.10
it
functionv0.1.0
- 2014.01.08
v1.0.0
- some new features and updates (based on requests from Issues)