-
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add .toBeJsonMatching(expectation) matcher
- Loading branch information
Showing
10 changed files
with
523 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { matchesObject, tryParseJSON } from '../utils'; | ||
|
||
export function toBeJsonMatching(actual, expected) { | ||
const { printExpected, printReceived, matcherHint } = this.utils; | ||
|
||
const parsed = tryParseJSON(actual); | ||
const isValidJSON = typeof parsed !== 'undefined'; | ||
|
||
const passMessage = | ||
`${matcherHint('.not.toBeJsonMatching')}\n\n` + | ||
`Expected input to not be a JSON string containing:\n ${printExpected(expected)}\n` + | ||
`${isValidJSON ? `Received:\n ${printReceived(parsed)}` : `Received invalid JSON:\n ${printReceived(actual)}`}`; | ||
|
||
const failMessage = | ||
`${matcherHint('.toBeJsonMatching')}\n\n` + | ||
`Expected input to be a JSON string containing:\n ${printExpected(expected)}\n` + | ||
`${isValidJSON ? `Received:\n ${printReceived(parsed)}` : `Received invalid JSON:\n ${printReceived(actual)}`}`; | ||
|
||
const pass = | ||
typeof actual === 'string' && | ||
typeof tryParseJSON(actual) !== 'undefined' && | ||
matchesObject(this.equals, tryParseJSON(actual), expected); | ||
|
||
return { pass, message: () => (pass ? passMessage : failMessage) }; | ||
} |
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
Oops, something went wrong.