Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-json

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-json - npm Package Compare versions

Comparing version 1.0.4 to 1.1.0

2

index.d.ts

@@ -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)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc