From 42c142565d82202ef4f8e554afc7d212c04b4803 Mon Sep 17 00:00:00 2001 From: Evans Hauser Date: Wed, 29 May 2019 10:52:52 -0700 Subject: [PATCH 1/2] test-link: add stringification to equality check Currently MockLink hides undefined values in its error message. This means that debugging a mismatched result was difficult and required stepping through the code. There are a couple of possible solutions: * update the error message to include the undefined values * modify the `isEquals` comparison of variables to check equality of the stringified value This chooses the latter, since the 99% path for receiving values from a remote source requires serializing variables before using them. --- Changelog.md | 3 +++ package-lock.json | 68 ++++++++++++++++++++++++----------------------- package.json | 2 ++ src/test-links.ts | 3 ++- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/Changelog.md b/Changelog.md index e5c0d8b9cd..38c642d6d2 100644 --- a/Changelog.md +++ b/Changelog.md @@ -13,6 +13,9 @@ - The result of a mutation is now made available to the wrapped component, when using the `graphql` HOC.
[@andycarrell](https://github.com/andycarrell) in [#3008](https://github.com/apollographql/react-apollo/pull/3008) +- Check equality of stringified variables in the `MockLink` to improve + debugging experience used by `MockedProvider`.
+ [@evans](https://github.com/evans) in [#3078](https://github.com/apollographql/react-apollo/pull/3078) ### Bug Fixes diff --git a/package-lock.json b/package-lock.json index d27e59e9fe..ffbaec27ac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1004,6 +1004,12 @@ "integrity": "sha512-yALhelO3i0hqZwhjtcr6dYyaLoCHbAMshwtj6cGxTvHZAKXHsYGdff6E8EPw3xLKY0ELUTQ69Q1rQiJENnccMA==", "dev": true }, + "@types/json-stable-stringify": { + "version": "1.0.32", + "resolved": "https://registry.npmjs.org/@types/json-stable-stringify/-/json-stable-stringify-1.0.32.tgz", + "integrity": "sha512-q9Q6+eUEGwQkv4Sbst3J4PNgDOvpuVuKj79Hl/qnmBMEIPzB5QoFRUtjcgcg2xNUZyYUGXBk5wYIBKHt0A+Mxw==", + "dev": true + }, "@types/lodash": { "version": "4.14.123", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.123.tgz", @@ -3209,7 +3215,7 @@ }, "minimist": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true }, @@ -3653,7 +3659,7 @@ }, "es6-promisify": { "version": "5.0.0", - "resolved": "https://registry.npmjs.org/es6-promisify/-/es6-promisify-5.0.0.tgz", + "resolved": "http://registry.npmjs.org/es6-promisify/-/es6-promisify-5.0.0.tgz", "integrity": "sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM=", "dev": true, "requires": { @@ -4130,8 +4136,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -4152,14 +4157,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -4174,20 +4177,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -4304,8 +4304,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -4317,7 +4316,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -4332,7 +4330,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -4340,14 +4337,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -4366,7 +4361,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -4447,8 +4441,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -4460,7 +4453,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -4546,8 +4538,7 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -4583,7 +4574,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -4603,7 +4593,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -4647,14 +4636,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, @@ -7451,6 +7438,15 @@ "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==", "dev": true }, + "json-stable-stringify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/json-stable-stringify/-/json-stable-stringify-1.0.1.tgz", + "integrity": "sha1-mnWdOcXy/1A/1TAGRu1EX4jE+a8=", + "dev": true, + "requires": { + "jsonify": "~0.0.0" + } + }, "json-stringify-safe": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", @@ -7472,6 +7468,12 @@ "graceful-fs": "^4.1.6" } }, + "jsonify": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", + "integrity": "sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=", + "dev": true + }, "jsonpointer": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/jsonpointer/-/jsonpointer-4.0.1.tgz", @@ -10436,7 +10438,7 @@ }, "minimist": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true }, diff --git a/package.json b/package.json index b3235a5f34..81bbcba0ed 100644 --- a/package.json +++ b/package.json @@ -113,6 +113,7 @@ "@types/hoist-non-react-statics": "3.3.1", "@types/invariant": "2.2.29", "@types/jest": "24.0.13", + "@types/json-stable-stringify": "^1.0.32", "@types/lodash.isequal": "4.5.5", "@types/object-assign": "4.0.30", "@types/prop-types": "15.7.1", @@ -138,6 +139,7 @@ "jest": "24.8.0", "jest-junit": "6.4.0", "jsdom": "14.1.0", + "json-stable-stringify": "^1.0.1", "lodash.includes": "4.3.0", "lodash.times": "4.3.2", "preact": "8.4.2", diff --git a/src/test-links.ts b/src/test-links.ts index d7e48b2b94..ffba4532d2 100644 --- a/src/test-links.ts +++ b/src/test-links.ts @@ -6,6 +6,7 @@ import { Observable, // Observer, } from 'apollo-link'; +import stringify from 'json-stable-stringify'; import { print } from 'graphql/language/printer'; import { @@ -67,7 +68,7 @@ export class MockLink extends ApolloLink { const response = (this.mockedResponsesByKey[key] || []).find((res, index) => { const requestVariables = operation.variables || {}; const mockedResponseVariables = res.request.variables || {}; - if (!isEqual(requestVariables, mockedResponseVariables)) { + if (!isEqual(stringify(requestVariables), stringify(mockedResponseVariables))) { return false; } responseIndex = index; From 6e0769671f5656af83ec376516036bd22c3d8c12 Mon Sep 17 00:00:00 2001 From: hwillson Date: Mon, 17 Jun 2019 12:42:23 -0400 Subject: [PATCH 2/2] Replace `json-stable-stringify` with `fast-json-stable-stringify` --- package-lock.json | 27 ++++++--------------------- package.json | 4 ++-- src/test-links.ts | 2 +- 3 files changed, 9 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index b2929344fa..9ffa393d77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -942,6 +942,12 @@ "integrity": "sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==", "dev": true }, + "@types/fast-json-stable-stringify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@types/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz", + "integrity": "sha512-mky/O83TXmGY39P1H9YbUpjV6l6voRYlufqfFCvel8l1phuy8HRjdWc1rrPuN53ITBJlbyMSV6z3niOySO5pgQ==", + "dev": true + }, "@types/graphql": { "version": "14.2.0", "resolved": "https://registry.npmjs.org/@types/graphql/-/graphql-14.2.0.tgz", @@ -1004,12 +1010,6 @@ "integrity": "sha512-yALhelO3i0hqZwhjtcr6dYyaLoCHbAMshwtj6cGxTvHZAKXHsYGdff6E8EPw3xLKY0ELUTQ69Q1rQiJENnccMA==", "dev": true }, - "@types/json-stable-stringify": { - "version": "1.0.32", - "resolved": "https://registry.npmjs.org/@types/json-stable-stringify/-/json-stable-stringify-1.0.32.tgz", - "integrity": "sha512-q9Q6+eUEGwQkv4Sbst3J4PNgDOvpuVuKj79Hl/qnmBMEIPzB5QoFRUtjcgcg2xNUZyYUGXBk5wYIBKHt0A+Mxw==", - "dev": true - }, "@types/lodash": { "version": "4.14.123", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.123.tgz", @@ -7490,15 +7490,6 @@ "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==", "dev": true }, - "json-stable-stringify": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/json-stable-stringify/-/json-stable-stringify-1.0.1.tgz", - "integrity": "sha1-mnWdOcXy/1A/1TAGRu1EX4jE+a8=", - "dev": true, - "requires": { - "jsonify": "~0.0.0" - } - }, "json-stringify-safe": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", @@ -7520,12 +7511,6 @@ "graceful-fs": "^4.1.6" } }, - "jsonify": { - "version": "0.0.0", - "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", - "integrity": "sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=", - "dev": true - }, "jsonpointer": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/jsonpointer/-/jsonpointer-4.0.1.tgz", diff --git a/package.json b/package.json index 728dda4279..d1f2ecb200 100644 --- a/package.json +++ b/package.json @@ -109,11 +109,11 @@ "devDependencies": { "@types/enzyme": "3.9.3", "@types/enzyme-adapter-react-16": "1.0.5", + "@types/fast-json-stable-stringify": "^2.0.0", "@types/graphql": "14.2.0", "@types/hoist-non-react-statics": "3.3.1", "@types/invariant": "2.2.29", "@types/jest": "24.0.14", - "@types/json-stable-stringify": "^1.0.32", "@types/lodash.isequal": "4.5.5", "@types/object-assign": "4.0.30", "@types/prop-types": "15.7.1", @@ -139,7 +139,6 @@ "jest": "24.8.0", "jest-junit": "6.4.0", "jsdom": "14.1.0", - "json-stable-stringify": "^1.0.1", "lodash.includes": "4.3.0", "lodash.times": "4.3.2", "preact": "8.4.2", @@ -167,6 +166,7 @@ }, "dependencies": { "apollo-utilities": "^1.3.0", + "fast-json-stable-stringify": "^2.0.0", "hoist-non-react-statics": "^3.3.0", "lodash.isequal": "^4.5.0", "prop-types": "^15.7.2", diff --git a/src/test-links.ts b/src/test-links.ts index ffba4532d2..ec1148b210 100644 --- a/src/test-links.ts +++ b/src/test-links.ts @@ -6,7 +6,7 @@ import { Observable, // Observer, } from 'apollo-link'; -import stringify from 'json-stable-stringify'; +import stringify from 'fast-json-stable-stringify'; import { print } from 'graphql/language/printer'; import {