-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: define matchers in separate files
- Loading branch information
1 parent
8c96607
commit 7339029
Showing
4 changed files
with
58 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import toMatchObject from './toMatchObject' | ||
|
||
export default [ | ||
toMatchObject, | ||
] |
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,27 @@ | ||
import { AssertionError, util } from 'chai' | ||
import { defineAssertion } from '../utils' | ||
import { getObjectSubset, iterableEquality, equals as jestEquals, subsetEquality } from '../jest-utils' | ||
import { getCustomEqualityTesters } from '../jest-matcher-utils' | ||
|
||
export default defineAssertion('toMatchObject', function (expected: unknown) { | ||
const customTesters = getCustomEqualityTesters() | ||
const actual = this._obj | ||
const pass = jestEquals(actual, expected, [...customTesters, iterableEquality, subsetEquality]) | ||
const isNot = util.flag(this, 'negate') as boolean | ||
const { subset: actualSubset, stripped } = getObjectSubset(actual, expected) | ||
if ((pass && isNot) || (!pass && !isNot)) { | ||
const msg = util.getMessage( | ||
this, | ||
[ | ||
pass, | ||
'expected #{this} to match object #{exp}', | ||
'expected #{this} to not match object #{exp}', | ||
expected, | ||
actualSubset, | ||
false, | ||
], | ||
) | ||
const message = stripped === 0 ? msg : `${msg}\n(${stripped} matching ${stripped === 1 ? 'property' : 'properties'} omitted from actual)` | ||
throw new AssertionError(message, { showDiff: true, expected, actual: actualSubset }) | ||
} | ||
}) |
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