From c34c897c39eb47c9c9d2ada0a9b8629ad661c991 Mon Sep 17 00:00:00 2001 From: cpojer Date: Wed, 1 Mar 2017 15:04:57 +0000 Subject: [PATCH] Improve printing of `expect.assertions` error. --- .../jest-jasmine2/src/setup-jest-globals.js | 25 ++++++++++++------- packages/jest-matchers/src/index.js | 4 +-- types/Matchers.js | 2 +- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/jest-jasmine2/src/setup-jest-globals.js b/packages/jest-jasmine2/src/setup-jest-globals.js index 47f04ff564a0..77f804f93616 100644 --- a/packages/jest-jasmine2/src/setup-jest-globals.js +++ b/packages/jest-jasmine2/src/setup-jest-globals.js @@ -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, }); } diff --git a/packages/jest-matchers/src/index.js b/packages/jest-matchers/src/index.js index f32277e456bd..715c70fbd622 100644 --- a/packages/jest-matchers/src/index.js +++ b/packages/jest-matchers/src/index.js @@ -48,8 +48,8 @@ if (!global[GLOBAL_STATE]) { {value: { matchers: Object.create(null), state: { + assertionCalls: 0, assertionsExpected: null, - assertionsMade: 0, suppressedErrors: [], }, }}, @@ -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); diff --git a/types/Matchers.js b/types/Matchers.js index 43e2f62e3412..8c0d14c684ac 100644 --- a/types/Matchers.js +++ b/types/Matchers.js @@ -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, };