Skip to content

Commit

Permalink
Force message in matchers to always be a function (jestjs#3972)
Browse files Browse the repository at this point in the history
* Force message in matchers to always be a function

* Rename variable back to message instead of messageFn
  • Loading branch information
jseminck authored and cpojer committed Jul 6, 2017
1 parent 645aec4 commit adb1505
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
8 changes: 4 additions & 4 deletions packages/jest-matchers/src/__tests__/extend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ jestExpect.extend({
toBeDivisibleBy(actual, expected) {
const pass = actual % expected === 0;
const message = pass
? `expected ${actual} not to be divisible by ${expected}`
: `expected ${actual} to be divisible by ${expected}`;
? () => `expected ${actual} not to be divisible by ${expected}`
: () => `expected ${actual} to be divisible by ${expected}`;

return {message, pass};
},
Expand All @@ -38,8 +38,8 @@ it('exposes matcherUtils in context', () => {
_shouldNotError(actual, expected) {
const pass = this.utils === matcherUtils;
const message = pass
? `expected this.utils to be defined in an extend call`
: `expected this.utils not to be defined in an extend call`;
? () => `expected this.utils to be defined in an extend call`
: () => `expected this.utils not to be defined in an extend call`;

return {message, pass};
},
Expand Down
16 changes: 4 additions & 12 deletions packages/jest-matchers/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,10 @@ const expect = (actual: any): ExpectationObject => {
};

const getMessage = message => {
// for performance reasons some of the messages are evaluated
// lazily
if (typeof message === 'function') {
message = message();
}

if (!message) {
message = utils.RECEIVED_COLOR(
'No message was specified for this matcher.',
);
}
return message;
return (
(message && message()) ||
utils.RECEIVED_COLOR('No message was specified for this matcher.')
);
};

const makeResolveMatcher = (
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-matchers/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,8 @@ const matchers: MatchersObject = {
const traversedPath = result.traversedPath.join('.');

const message = pass
? matcherHint('.not.toHaveProperty', 'object', 'path', {
? () =>
matcherHint('.not.toHaveProperty', 'object', 'path', {
secondArgument: valuePassed ? 'value' : null,
}) +
'\n\n' +
Expand All @@ -552,7 +553,8 @@ const matchers: MatchersObject = {
`Not to have a nested property:\n` +
` ${printExpected(keyPath)}\n` +
(valuePassed ? `With a value of:\n ${printExpected(value)}\n` : '')
: matcherHint('.toHaveProperty', 'object', 'path', {
: () =>
matcherHint('.toHaveProperty', 'object', 'path', {
secondArgument: valuePassed ? 'value' : null,
}) +
'\n\n' +
Expand Down
2 changes: 1 addition & 1 deletion types/Matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {SnapshotState} from 'jest-snapshot';

export type ExpectationResult = {
pass: boolean,
message: string | (() => string),
message: () => string,
};

export type RawMatcherFn = (
Expand Down

0 comments on commit adb1505

Please sign in to comment.