Reputation: 377
I found this code snippet from Angular's document, but cannot find this usage from Jasmine's docs. Is this really a supported feature? I just want to make sure that if I use this feature it will not break unexpectedly when upgrading to a newer minor/patch version.
expect(masterService.getValue())
.toBe(stubValue, 'service returned stub value');
Upvotes: 3
Views: 2467
Reputation: 1568
The supported method since 3.3 is withContext: https://jasmine.github.io/api/edge/matchers.html#withContext
expect(masterService.getValue()).withContext('service returned stub value')
.toBe(stubValue);
Upvotes: 6
Reputation: 377
Important Update:
I found passing in the second argument doesn't work for .toEqual() although it works for .toBe(). I'm using jasmine
2.8.16, haven't tried on other versions.
I also found these relevant threads:
https://github.com/jasmine/jasmine/issues/641
https://github.com/adobe/brackets/issues/2752
So now I have changed my mind and would recommend NOT using this feature in your project. If you really want to use it, at least double check to make sure it works with all the needed matchers in your jasmine
version.
Original Answer
I found an explanation of this in a different place in Angular's docs, although I still don't see it in Jasmine's docs. With @uminder's response together I feel most likely it's fine to use it. Feel free to give more updates if anyone has anything else to add.
The second parameter to the Jasmine matcher (e.g., 'expected name') is an optional failure label. If the expectation fails, Jasmine appends this label to the expectation failure message. In a spec with multiple expectations, it can help clarify what went wrong and which expectation failed.
Upvotes: 1
Reputation: 26150
The second optional argument of jasmine.Matchers.toBe
(named expectationFailOutput
) is the message to be displayed when expect
fails. Regardless of not appearing in the Jasmine documentation, it is really supported and exists on most methods of the interface jasmine.Matchers
.
interface Matchers {
...
toBe(expected: any, expectationFailOutput?: any): boolean;
toEqual(expected: any, expectationFailOutput?: any): boolean;
toMatch(expected: string | RegExp, expectationFailOutput?: any): boolean;
toBeDefined(expectationFailOutput?: any): boolean;
toBeUndefined(expectationFailOutput?: any): boolean;
toBeNull(expectationFailOutput?: any): boolean;
...
toBeTruthy(expectationFailOutput?: any): boolean;
toBeFalsy(expectationFailOutput?: any): boolean;
...
toContain(expected: any, expectationFailOutput?: any): boolean;
toBeLessThan(expected: number, expectationFailOutput?: any): boolean;
toBeLessThanOrEqual(expected: number, expectationFailOutput?: any): boolean;
toBeGreaterThan(expected: number, expectationFailOutput?: any): boolean;
toBeGreaterThanOrEqual(expected: number, expectationFailOutput?: any): boolean;
toBeCloseTo(expected: number, precision?: any, expectationFailOutput?: any): boolean;
Upvotes: 2