-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8719053
commit 005140f
Showing
8 changed files
with
163 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/jest-matchers/src/extractExpectedAssertionsErrors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright (c) 2014, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
|
||
const { | ||
EXPECTED_COLOR, | ||
RECEIVED_COLOR, | ||
matcherHint, | ||
pluralize, | ||
} = require('jest-matcher-utils'); | ||
|
||
const {getState, setState} = require('./global_object'); | ||
|
||
// Create and format all errors related to the mismatched number of `expect` | ||
// calls and reset the matchers state. | ||
const extractExpectedAssertionsErrors = () => { | ||
const result = []; | ||
const { | ||
assertionCalls, | ||
expectedAssertionsNumber, | ||
isExpectingAssertions, | ||
} = getState(); | ||
setState({assertionCalls: 0, expectedAssertionsNumber: null}); | ||
if ( | ||
typeof expectedAssertionsNumber === 'number' && | ||
assertionCalls !== expectedAssertionsNumber | ||
) { | ||
const numOfAssertionsExpected = EXPECTED_COLOR( | ||
pluralize('assertion', expectedAssertionsNumber), | ||
); | ||
const error = new Error( | ||
matcherHint('.assertions', '', String(expectedAssertionsNumber), { | ||
isDirectExpectCall: true, | ||
}) + | ||
'\n\n' + | ||
`Expected ${numOfAssertionsExpected} to be called but only received ` + | ||
RECEIVED_COLOR(pluralize('assertion call', assertionCalls || 0)) + | ||
'.', | ||
); | ||
result.push(error); | ||
} | ||
if (isExpectingAssertions && assertionCalls === 0) { | ||
const expected = EXPECTED_COLOR('at least one assertion'); | ||
const received = RECEIVED_COLOR('received none'); | ||
const error = new Error( | ||
matcherHint('.hasAssertions', '', '', { | ||
isDirectExpectCall: true, | ||
}) + | ||
'\n\n' + | ||
`Expected ${expected} to be called but ${received}.`, | ||
); | ||
result.push(error); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
module.exports = extractExpectedAssertionsErrors; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright (c) 2014, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {MatchersObject} from 'types/Matchers'; | ||
|
||
// Global matchers object holds the list of available matchers and | ||
// the state, that can hold matcher specific values that change over time. | ||
const JEST_MATCHERS_OBJECT = Symbol.for('$$jest-matchers-object'); | ||
|
||
if (!global[JEST_MATCHERS_OBJECT]) { | ||
Object.defineProperty(global, JEST_MATCHERS_OBJECT, { | ||
value: { | ||
matchers: Object.create(null), | ||
state: { | ||
assertionCalls: 0, | ||
expectedAssertionsNumber: null, | ||
isExpectingAssertions: false, | ||
suppressedErrors: [], // errors that are not thrown immediately. | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
const getState = () => global[JEST_MATCHERS_OBJECT].state; | ||
|
||
const setState = (state: Object) => { | ||
Object.assign(global[JEST_MATCHERS_OBJECT].state, state); | ||
}; | ||
|
||
const getMatchers = () => global[JEST_MATCHERS_OBJECT].matchers; | ||
|
||
const setMatchers = (matchers: MatchersObject) => { | ||
Object.assign(global[JEST_MATCHERS_OBJECT].matchers, matchers); | ||
}; | ||
|
||
module.exports = { | ||
getMatchers, | ||
getState, | ||
setMatchers, | ||
setState, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters