Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit 158b428

Browse files
committed
Improve mismatched query error message
1 parent b3af031 commit 158b428

File tree

4 files changed

+127
-38
lines changed

4 files changed

+127
-38
lines changed

package-lock.json

+11-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@
166166
"dependencies": {
167167
"apollo-utilities": "^1.2.1",
168168
"hoist-non-react-statics": "^3.0.0",
169-
"lodash.isequal": "^4.5.0",
169+
"jest-diff": "^24.5.0",
170+
"lodash": "^4.17.11",
170171
"prop-types": "^15.6.0",
171172
"ts-invariant": "^0.3.0",
172173
"tslib": "^1.9.3"

src/test-links.ts

+41-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import {
88
} from 'apollo-link';
99

1010
import { print } from 'graphql/language/printer';
11+
import { getOperationAST } from 'graphql';
1112
import {
1213
addTypenameToDocument,
1314
removeClientSetsFromDocument,
1415
removeConnectionDirectiveFromDocument,
1516
cloneDeep,
1617
} from 'apollo-utilities';
17-
const isEqual = require('lodash.isequal');
18+
import values from 'lodash/values';
19+
import isEqual from 'lodash/isEqual';
20+
import diff from 'jest-diff';
1821

1922
type ResultFunction<T> = () => T;
2023

@@ -75,10 +78,18 @@ export class MockLink extends ApolloLink {
7578
});
7679

7780
if (!response || typeof responseIndex === 'undefined') {
81+
const queryDiffs = (<string[]> []).concat(
82+
...values(this.mockedResponsesByKey).map(mockedResponses =>
83+
mockedResponses.map(mockedResponse =>
84+
diffRequest(mockedResponse.request, operation, this.addTypename),
85+
),
86+
),
87+
);
88+
7889
throw new Error(
79-
`No more mocked responses for the query: ${print(
80-
operation.query,
81-
)}, variables: ${JSON.stringify(operation.variables)}`,
90+
`No more mocked responses for ${requestToString(operation)}${
91+
queryDiffs.length ? `\n\nPossible matches:\n${queryDiffs.join('\n')}` : ''
92+
}`,
8293
);
8394
}
8495

@@ -178,6 +189,32 @@ function requestToKey(request: GraphQLRequest, addTypename: Boolean): string {
178189
return JSON.stringify(requestKey);
179190
}
180191

192+
function diffRequest(
193+
actualRequest: GraphQLRequest,
194+
expectedRequest: GraphQLRequest,
195+
addTypename?: Boolean
196+
): string {
197+
return diff(
198+
requestToString(actualRequest, addTypename),
199+
requestToString(expectedRequest)
200+
) || '';
201+
}
202+
203+
function requestToString(
204+
request: GraphQLRequest,
205+
addTypename?: Boolean
206+
): string {
207+
const query = print(
208+
addTypename ? addTypenameToDocument(request.query) : request.query
209+
);
210+
const variables = request.variables
211+
? JSON.stringify(request.variables, null, 2)
212+
: '{}';
213+
const operationAST = getOperationAST(request.query, null);
214+
const operationName = operationAST ? operationAST.operation : 'query';
215+
return `${operationName}:\n${query}variables:\n${variables}`;
216+
}
217+
181218
// Pass in multiple mocked responses, so that you can test flows that end up
182219
// making multiple queries to the server
183220
// NOTE: The last arg can optionally be an `addTypename` arg

test/__snapshots__/test-utils.test.tsx.snap

+73-6
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,100 @@ Object {
1515
`;
1616

1717
exports[`errors if the query in the mock and component do not match 1`] = `
18-
[Error: Network error: No more mocked responses for the query: query GetUser($username: String!) {
18+
[Error: Network error: No more mocked responses for query:
19+
query GetUser($username: String!) {
1920
user(username: $username) {
2021
id
2122
__typename
2223
}
2324
}
24-
, variables: {"username":"mock_username"}]
25+
variables:
26+
{
27+
"username": "mock_username"
28+
}
29+
30+
Possible matches:
31+
- Expected
32+
+ Received
33+
34+
 query:
35+
- query OtherQuery {
36+
- otherQuery {
37+
+ query GetUser($username: String!) {
38+
+ user(username: $username) {
39+
 id
40+
 __typename
41+
 }
42+
 }
43+
 variables:
44+
 {
45+
 "username": "mock_username"
46+
 }]
2547
`;
2648
2749
exports[`errors if the variables do not deep equal 1`] = `
28-
[Error: Network error: No more mocked responses for the query: query GetUser($username: String!) {
50+
[Error: Network error: No more mocked responses for query:
51+
query GetUser($username: String!) {
2952
user(username: $username) {
3053
id
3154
__typename
3255
}
3356
}
34-
, variables: {"username":"some_user","age":42}]
57+
variables:
58+
{
59+
"username": "some_user",
60+
"age": 42
61+
}
62+
63+
Possible matches:
64+
- Expected
65+
+ Received
66+
67+
 query:
68+
 query GetUser($username: String!) {
69+
 user(username: $username) {
70+
 id
71+
 __typename
72+
 }
73+
 }
74+
 variables:
75+
 {
76+
- "age": 13,
77+
- "username": "some_user"
78+
+ "username": "some_user",
79+
+ "age": 42
80+
 }]
3581
`;
3682
3783
exports[`errors if the variables in the mock and component do not match 1`] = `
38-
[Error: Network error: No more mocked responses for the query: query GetUser($username: String!) {
84+
[Error: Network error: No more mocked responses for query:
85+
query GetUser($username: String!) {
3986
user(username: $username) {
4087
id
4188
__typename
4289
}
4390
}
44-
, variables: {"username":"other_user"}]
91+
variables:
92+
{
93+
"username": "other_user"
94+
}
95+
96+
Possible matches:
97+
- Expected
98+
+ Received
99+
100+
 query:
101+
 query GetUser($username: String!) {
102+
 user(username: $username) {
103+
 id
104+
 __typename
105+
 }
106+
 }
107+
 variables:
108+
 {
109+
- "username": "mock_username"
110+
+ "username": "other_user"
111+
 }]
45112
`;
46113
47114
exports[`mocks the data 1`] = `

0 commit comments

Comments
 (0)