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

Commit 595c2ce

Browse files
committed
Improve mismatched query error message
1 parent b3af031 commit 595c2ce

File tree

4 files changed

+130
-38
lines changed

4 files changed

+130
-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\n${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

+76-6
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,103 @@ 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
19+
query:
20+
query GetUser($username: String!) {
1921
user(username: $username) {
2022
id
2123
__typename
2224
}
2325
}
24-
, variables: {"username":"mock_username"}]
26+
variables:
27+
{
28+
"username": "mock_username"
29+
}
30+
31+
Possible matches:
32+
- Expected
33+
+ Received
34+
35+
 query:
36+
- query OtherQuery {
37+
- otherQuery {
38+
+ query GetUser($username: String!) {
39+
+ user(username: $username) {
40+
 id
41+
 __typename
42+
 }
43+
 }
44+
 variables:
45+
 {
46+
 "username": "mock_username"
47+
 }]
2548
`;
2649
2750
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!) {
51+
[Error: Network error: No more mocked responses for
52+
query:
53+
query GetUser($username: String!) {
2954
user(username: $username) {
3055
id
3156
__typename
3257
}
3358
}
34-
, variables: {"username":"some_user","age":42}]
59+
variables:
60+
{
61+
"username": "some_user",
62+
"age": 42
63+
}
64+
65+
Possible matches:
66+
- Expected
67+
+ Received
68+
69+
 query:
70+
 query GetUser($username: String!) {
71+
 user(username: $username) {
72+
 id
73+
 __typename
74+
 }
75+
 }
76+
 variables:
77+
 {
78+
- "age": 13,
79+
- "username": "some_user"
80+
+ "username": "some_user",
81+
+ "age": 42
82+
 }]
3583
`;
3684
3785
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!) {
86+
[Error: Network error: No more mocked responses for
87+
query:
88+
query GetUser($username: String!) {
3989
user(username: $username) {
4090
id
4191
__typename
4292
}
4393
}
44-
, variables: {"username":"other_user"}]
94+
variables:
95+
{
96+
"username": "other_user"
97+
}
98+
99+
Possible matches:
100+
- Expected
101+
+ Received
102+
103+
 query:
104+
 query GetUser($username: String!) {
105+
 user(username: $username) {
106+
 id
107+
 __typename
108+
 }
109+
 }
110+
 variables:
111+
 {
112+
- "username": "mock_username"
113+
+ "username": "other_user"
114+
 }]
45115
`;
46116
47117
exports[`mocks the data 1`] = `

0 commit comments

Comments
 (0)