Skip to content

Commit

Permalink
Improve printing of expect.assertions error.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer committed Mar 1, 2017
1 parent b9feb4a commit c34c897
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
25 changes: 16 additions & 9 deletions packages/jest-jasmine2/src/setup-jest-globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,29 @@ const addSuppressedErrors = result => {
};

const addAssertionErrors = result => {
const {assertionsMade, assertionsExpected} = getState();
setState({assertionsExpected: null, assertionsMade: 0});
const {assertionCalls, assertionsExpected} = getState();
setState({
assertionCalls: 0,
assertionsExpected: null,
});
if (
typeof assertionsExpected === 'number' &&
assertionsMade !== assertionsExpected
assertionCalls !== assertionsExpected
) {
const expected = EXPECTED_COLOR(pluralize('assertion', assertionsExpected));
const message = new Error(
matcherHint('.assertions', '', assertionsExpected, {
isDirectExpectCall: true,
}) +
'\n\n' +
`Expected ${expected} to be called but only received ` +
`${RECEIVED_COLOR(pluralize('assertion call', assertionCalls))}.`,
);
result.status = 'failed';
result.failedExpectations.push({
actual: assertionsMade,
actual: assertionCalls,
expected: assertionsExpected,
message: matcherHint('.assertions', '', assertionsExpected, {
isDirectExpectCall: true,
}) + '\n\n' +
`Expected: ${expected}\n` +
`Received: ${RECEIVED_COLOR(pluralize('assertion', assertionsMade))}`,
message,
passed: false,
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-matchers/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ if (!global[GLOBAL_STATE]) {
{value: {
matchers: Object.create(null),
state: {
assertionCalls: 0,
assertionsExpected: null,
assertionsMade: 0,
suppressedErrors: [],
},
}},
Expand Down Expand Up @@ -119,7 +119,7 @@ const makeThrowingMatcher = (

_validateResult(result);

global[GLOBAL_STATE].state.assertionsMade++;
global[GLOBAL_STATE].state.assertionCalls++;

if ((result.pass && isNot) || (!result.pass && !isNot)) { // XOR
const message = getMessage(result.message);
Expand Down
2 changes: 1 addition & 1 deletion types/Matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export type RawMatcherFn = (
export type ThrowingMatcherFn = (actual: any) => void;
export type MatcherContext = {isNot: boolean};
export type MatcherState = {
assertionCalls?: number,
assertionsExpected?: ?number,
assertionsMade?: number,
currentTestName?: string,
testPath?: Path,
};
Expand Down

0 comments on commit c34c897

Please sign in to comment.