Comparing version 1.0.4 to 1.1.0
@@ -0,1 +1,3 @@ | ||
/// <reference types="jest" /> | ||
declare namespace jest { | ||
@@ -2,0 +4,0 @@ interface Matchers<R> { |
180
index.js
"use strict"; | ||
const diff = require("jest-diff"); | ||
const { equals } = require("expect/build/jasmine_utils"); | ||
const { isOneline } = require("expect/build/utils"); | ||
const { | ||
RECEIVED_COLOR, | ||
matcherHint, | ||
printExpected, | ||
printReceived, | ||
printWithType | ||
} = require("jest-matcher-utils"); | ||
const { equals } = require("expect/build/jasmineUtils"); | ||
const errorMarker = "__INTERNALERRORMARKER__"; | ||
/** | ||
@@ -19,53 +12,71 @@ * Jest matcher that receives a JSON string and matches to a value. | ||
*/ | ||
function toMatchJSON(actual, expected) { | ||
const hint = matcherHint(".toMatchJSON", "string", "expected", { | ||
isNot: this.isNot | ||
function toMatchJSON(received, expected) { | ||
const { | ||
matcherErrorMessage, | ||
RECEIVED_COLOR, | ||
printDiffOrStringify, | ||
printExpected, | ||
printReceived, | ||
printWithType, | ||
stringify | ||
} = this.utils; | ||
const hint = this.utils.matcherHint("toMatchJSON", undefined, undefined, { | ||
isNot: this.isNot, | ||
promise: this.promise | ||
}); | ||
const prefix = hint + "\n\n" + RECEIVED_COLOR("string") + " "; | ||
if (typeof actual !== "string") { | ||
if (typeof received !== "string") { | ||
throwError( | ||
prefix + | ||
"value must be a string.\n" + | ||
printWithType("Received", actual, printReceived) | ||
matcherErrorMessage( | ||
hint, | ||
`${RECEIVED_COLOR("received")} value must be a valid JSON string`, | ||
printWithType("Received", received, printReceived) | ||
) | ||
); | ||
} | ||
if (!actual) { | ||
try { | ||
received = JSON.parse(received); | ||
} catch (error) { | ||
const match = error.message.match( | ||
/Unexpected (\w+)(?: .)? in JSON at position (\d+)/ | ||
); | ||
const index = match ? parseInt(match[2], 10) : received.length; | ||
const isEmpty = received.trim().length === 0; | ||
const message = isEmpty | ||
? "" | ||
: match | ||
? `Unexpected ${match[1]}: ${received[index]}` | ||
: "Unexpected end of string"; | ||
throwError( | ||
prefix + | ||
"value must be a valid JSON.\nReceived:\n " + | ||
printReceived(actual) | ||
matcherErrorMessage( | ||
hint, | ||
`${RECEIVED_COLOR( | ||
"received" | ||
)} value must be a valid JSON string. ${message}`, | ||
isEmpty | ||
? "Received: " + RECEIVED_COLOR(stringify(received)) | ||
: printJsonError(stringify(received), RECEIVED_COLOR, index + 1) | ||
) | ||
); | ||
} | ||
try { | ||
actual = JSON.parse(actual); | ||
} catch (err) { | ||
throwError( | ||
prefix + | ||
"value must be a valid JSON.\n" + | ||
printInvalid(actual, err.message) | ||
); | ||
} | ||
const pass = equals(actual, expected); | ||
const pass = equals(received, expected); | ||
const message = pass | ||
? () => | ||
matcherHint(".not.toMatchJSON") + | ||
"\n\nExpected value not to match:\n " + | ||
printExpected(expected) + | ||
"\nReceived:\n " + | ||
printReceived(actual) | ||
`${hint} \n\nExpected: not ${printExpected(expected)}` + | ||
(stringify(expected) !== stringify(received) | ||
? `\nReceived: ${printReceived(received)}` | ||
: "") | ||
: () => { | ||
const oneline = isOneline(expected, actual); | ||
const diffString = | ||
!oneline && diff(expected, actual, { expand: this.expand }); | ||
return ( | ||
matcherHint(".toMatchJSON") + | ||
"\n\nExpected value to match:\n " + | ||
printExpected(expected) + | ||
"\nReceived:\n " + | ||
printReceived(actual) + | ||
(diffString && !oneline ? "\n\nDifference: \n\n" + diffString : "") | ||
`${hint} \n\n` + | ||
printDiffOrStringify( | ||
expected, | ||
received, | ||
"Expected", | ||
"Received", | ||
this.expand !== false | ||
) | ||
); | ||
@@ -84,15 +95,9 @@ }; | ||
*/ | ||
function jsonMatching(actual, expected) { | ||
const _this = expect.jsonMatching(); | ||
if (typeof actual !== "string") { | ||
throw Error( | ||
`You must provide a string to ${_this.toString()}, not '${typeof actual}'.` | ||
); | ||
} | ||
function jsonMatching(received, expected) { | ||
let pass = false; | ||
try { | ||
actual = JSON.parse(actual); | ||
} catch (err) { | ||
throw Error("Actual is not valid JSON"); | ||
} | ||
return { pass: equals(actual, expected) }; | ||
received = JSON.parse(received); | ||
pass = equals(received, expected); | ||
} catch (err) {} // eslint-disable-line no-empty | ||
return { pass }; | ||
} | ||
@@ -105,24 +110,17 @@ | ||
*/ | ||
function printInvalid(received, error) { | ||
const match = error.match( | ||
/Unexpected (\w+)(?: .)? in JSON at position (\d+)/ | ||
); | ||
const message = "Received:\n " + printReceived(received) + "\n"; | ||
if (match) { | ||
const pos = parseInt(match[2], 10); | ||
return ( | ||
message + | ||
" ".repeat(pos + 3) + | ||
"^\nUnexpected " + | ||
match[1] + | ||
": " + | ||
RECEIVED_COLOR(received[pos]) | ||
); | ||
function printJsonError(value, print, index) { | ||
let message = `Received: `; | ||
const lines = value.split("\n"); | ||
for (let i = 0, count = 0; i < lines.length; i++) { | ||
const line = lines[i]; | ||
message += print(line) + "\n"; | ||
if (index >= count && index <= count + line.length) { | ||
message += " ".repeat(index - count + (i === 0 ? 10 : 0)) + "^\n"; | ||
} | ||
count += line.length + 1; | ||
} | ||
return ( | ||
message + | ||
" ".repeat(received.length + 3) + | ||
"^\n" + | ||
"Unexpected end of string\n" | ||
); | ||
return message; | ||
} | ||
@@ -135,10 +133,20 @@ | ||
try { | ||
throw Error(message); | ||
throw Error(errorMarker); | ||
} catch (err) { | ||
message = message || "Error"; | ||
const stack = err.stack.slice(message.length).split("\n"); | ||
stack.splice(0, 4, message); | ||
err.stack = stack.join("\n"); | ||
throw err; | ||
const stack = err.stack | ||
.slice(err.stack.indexOf(errorMarker) + errorMarker.length) | ||
.split("\n"); | ||
for (let i = stack.length - 1; i > 0; i--) { | ||
// search for the "first" matcher call in the trace | ||
if (stack[i].includes("toMatchJSON")) { | ||
stack.splice(0, i + 1, message); | ||
break; | ||
} | ||
} | ||
const error = Error(message); | ||
error.stack = stack.join("\n"); | ||
throw error; | ||
} | ||
} |
{ | ||
"name": "jest-json", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "Jest matcher for working with JSON", | ||
@@ -20,19 +20,17 @@ "author": "Lucas Duailibe", | ||
"scripts": { | ||
"test": "eslint . && prettier --list-different README.md && jest" | ||
"lint": "eslint .", | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"expect": "^23.0.0", | ||
"jest-diff": "^23.0.0", | ||
"jest-matcher-utils": "^23.0.0" | ||
"expect": "^27.2.4" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^26.0.20", | ||
"eslint": "^5.1.0", | ||
"eslint-config-prettier": "^2.9.0", | ||
"eslint-plugin-prettier": "^2.6.2", | ||
"has-ansi": "^3.0.0", | ||
"jest": "^23.4.1", | ||
"prettier": "^1.13.7", | ||
"strip-ansi": "^4.0.0" | ||
"jest": "^27.2.4", | ||
"jest-snapshot-serializer-ansi": "^1.0.0", | ||
"jest-snapshot-serializer-raw": "^1.2.0", | ||
"prettier": "^1.13.7" | ||
} | ||
} |
# `jest-json` | ||
[![Travis](https://api.travis-ci.com/duailibe/jest-json.svg)](https://travis-ci.com/duailibe/jest-json) | ||
[![CI](https://img.shields.io/github/workflow/status/duailibe/jest-json/CI.svg)](https://github.com/duailibe/jest-json/actions/workflows/ci.yaml) | ||
[![Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) | ||
@@ -5,0 +5,0 @@ [![npm](https://img.shields.io/npm/v/jest-json.svg)](https://npmjs.org/jest-json) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6743
1
7
140
+ Added@jest/types@27.5.1(transitive)
+ Added@types/istanbul-lib-coverage@2.0.6(transitive)
+ Added@types/istanbul-lib-report@3.0.3(transitive)
+ Added@types/istanbul-reports@3.0.4(transitive)
+ Added@types/node@22.10.2(transitive)
+ Added@types/stack-utils@2.0.3(transitive)
+ Added@types/yargs@16.0.9(transitive)
+ Added@types/yargs-parser@21.0.3(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedansi-styles@4.3.05.2.0(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addeddiff-sequences@27.5.1(transitive)
+ Addedexpect@27.5.1(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedjest-diff@27.5.1(transitive)
+ Addedjest-get-type@27.5.1(transitive)
+ Addedjest-matcher-utils@27.5.1(transitive)
+ Addedjest-message-util@27.5.1(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedpretty-format@27.5.1(transitive)
+ Addedreact-is@17.0.2(transitive)
+ Addedslash@3.0.0(transitive)
+ Addedstack-utils@2.0.6(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)
+ Addedundici-types@6.20.0(transitive)
- Removedjest-diff@^23.0.0
- Removedjest-matcher-utils@^23.0.0
- Removedansi-regex@3.0.1(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedarr-diff@2.0.0(transitive)
- Removedarr-flatten@1.1.0(transitive)
- Removedarray-unique@0.2.1(transitive)
- Removedbraces@1.8.5(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removeddiff@3.5.0(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedexpand-brackets@0.1.5(transitive)
- Removedexpand-range@1.8.2(transitive)
- Removedexpect@23.6.0(transitive)
- Removedextglob@0.3.2(transitive)
- Removedfilename-regex@2.0.1(transitive)
- Removedfill-range@2.2.4(transitive)
- Removedfor-in@1.0.2(transitive)
- Removedfor-own@0.1.5(transitive)
- Removedglob-base@0.3.0(transitive)
- Removedglob-parent@2.0.0(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-dotfile@1.0.3(transitive)
- Removedis-equal-shallow@0.1.3(transitive)
- Removedis-extendable@0.1.1(transitive)
- Removedis-extglob@1.0.0(transitive)
- Removedis-glob@2.0.1(transitive)
- Removedis-number@2.1.04.0.0(transitive)
- Removedis-posix-bracket@0.1.1(transitive)
- Removedis-primitive@2.0.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisobject@2.1.0(transitive)
- Removedjest-diff@23.6.0(transitive)
- Removedjest-get-type@22.4.3(transitive)
- Removedjest-matcher-utils@23.6.0(transitive)
- Removedjest-message-util@23.4.0(transitive)
- Removedjest-regex-util@23.3.0(transitive)
- Removedkind-of@3.2.26.0.3(transitive)
- Removedmath-random@1.0.4(transitive)
- Removedmicromatch@2.3.11(transitive)
- Removednormalize-path@2.1.1(transitive)
- Removedobject.omit@2.0.1(transitive)
- Removedparse-glob@3.0.4(transitive)
- Removedpreserve@0.2.0(transitive)
- Removedpretty-format@23.6.0(transitive)
- Removedrandomatic@3.1.1(transitive)
- Removedregex-cache@0.4.4(transitive)
- Removedremove-trailing-separator@1.1.0(transitive)
- Removedrepeat-element@1.1.4(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedslash@1.0.0(transitive)
- Removedstack-utils@1.0.5(transitive)
- Removedsupports-color@5.5.0(transitive)
Updatedexpect@^27.2.4