jasmine2-custom-message icon indicating copy to clipboard operation
jasmine2-custom-message copied to clipboard

Custom messages for passed expectations

Open Sarkell opened this issue 7 years ago • 7 comments

Hi, I have just a simple question. Could this library be updated so that it can write a message for passed 'expect' also?

As far as I understand jasmine won't provide this feature in future. According to https://github.com/jasmine/jasmine/issues/1127

Nevertheless, I extremally need this ability.

Sarkell avatar Jan 16 '18 15:01 Sarkell

Hi, At the moment I have no plans for it.

avrelian avatar Jan 22 '18 16:01 avrelian

@Sarkell you can just

const myExpectVariable = sut.whateverFunction();
since(function() {
    return `got ${myExpectVariable} expected ${this.actual} to be ${this.expected}`;
}).expect(myCheck(myExpectVariable)).toBeWhatever();

you don't need a feature for that.

danielesegato avatar Jun 20 '18 10:06 danielesegato

@Sarkell @avrelian I'v solved this issue by overriding global jasmine functions - this code can be added in module as is :) Solution:

let jasmine = require('jasmine-core');

// Override build expectation result: Passed string problem
// https://github.com/jasmine/jasmine/issues/971
let buildExpectationResult = jasmine.buildExpectationResult();
jasmine.buildExpectationResult = function() {
	function buildExpectationResultWrapper(options) {
		let message = options ? options.message : '';
		let result = buildExpectationResult(options);
		if (result.passed && message) {
			result.message = message;
		}
		
		return result;
	}
	return buildExpectationResultWrapper;
}

cheplv avatar Jul 26 '18 15:07 cheplv

@cheplv , i'm new to protractor/jasmine. would you be able to show an example on how to use your wrapper? thank you!

rajeshpadmanaban avatar Jul 30 '18 15:07 rajeshpadmanaban

As an example of protractor.conf.js with override of Passed.

https://gist.github.com/cheplv/8801eaef7e807f55b17ff7cfe6b1d376

Launch with command like: NODE_ENV=dev protractorUrl="https://some-test.domain.com" node_modules/protractor/bin/protractor tests/e2e/protractor.conf.js

And use in spec like: since('User logged in').expect(User.isAuthenticated()).toBeTruthy();

cheplv avatar Aug 01 '18 14:08 cheplv

@cheplv thank you! I did use your override function. But I don't have the jasmine custom messages packages and hence i'm not able to use since. Tried except(A).toEqual(B, 'test') to see if the message test is displayed when the test passes. But it did not get displayed in the report. Am i missing something?

rajeshpadmanaban avatar Aug 16 '18 14:08 rajeshpadmanaban

@rajeshpadmanaban This thread is intended to solve jasmine2-custom-message since function success message. Add project to your dependencies and use as it intended :)

cheplv avatar Aug 19 '18 18:08 cheplv