Skip to content

Commit

Permalink
Improve printing of expect.assertions error. (jestjs#3033)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer authored Mar 2, 2017
1 parent aa0355e commit 0427832
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
16 changes: 8 additions & 8 deletions integration_tests/__tests__/__snapshots__/failures-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ Object {
● .assertions() › throws
Error
expect.assertions(2)
expect.assertions(2)
Expected two assertions to be called but only received one assertion call.
Expected: two assertions
Received: one assertion
at addAssertionErrors (../../packages/jest-jasmine2/build/setup-jest-globals.js:56:21)
● .assertions() › throws on redeclare of assertion count
Expand All @@ -83,11 +83,11 @@ Object {
● .assertions() › throws on assertion
Error
expect.assertions(0)
expect.assertions(0)
Expected zero assertions to be called but only received one assertion call.
Expected: zero assertions
Received: one assertion
at addAssertionErrors (../../packages/jest-jasmine2/build/setup-jest-globals.js:56:21)
.assertions()
throws
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ describe('.assertions()', () => {
it('throws on redeclare of assertion count', redeclare);
it('throws on assertion', noAssertions);
});

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))}.`,
).stack;
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 0427832

Please sign in to comment.