Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve 'No more mocked responses' error message #6963

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { InMemoryCache } from '../../../../cache/inmemory/inMemoryCache';
import { ApolloLink } from '../../../../link/core';

const variables = {
username: 'mock_username'
username: 'mock_username',
};

const userWithoutTypeName = {
Expand Down Expand Up @@ -154,7 +154,8 @@ describe('General use', () => {
}

const variables2 = {
username: 'other_user'
username: 'mock_username',
age: undefined
};

const link = ApolloLink.from([errorLink, new MockLink(mocks)]);
Expand Down Expand Up @@ -222,7 +223,7 @@ describe('General use', () => {
query,
variables: {
age: 13,
username: 'some_user'
username: 'some_user',
}
},
result: { data: { user } }
Expand All @@ -231,7 +232,7 @@ describe('General use', () => {

const variables2 = {
username: 'some_user',
age: 13
age: 13,
};

render(
Expand Down
21 changes: 17 additions & 4 deletions src/utilities/testing/mocking/mockLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class MockLink extends ApolloLink {
this.operation = operation;
const key = requestToKey(operation, this.addTypename);
let responseIndex: number = 0;
let diffs: Array<Record<string, any>> = [];
const response = (this.mockedResponsesByKey[key] || []).find(
(res, index) => {
const requestVariables = operation.variables || {};
Expand All @@ -81,16 +82,28 @@ export class MockLink extends ApolloLink {
responseIndex = index;
return true;
}
diffs.push(mockedResponseVariables);
return false;
}
);

if (!response || typeof responseIndex === 'undefined') {
this.onError(new Error(
const replacer = (key: string, value: any) =>
typeof value === 'undefined' ? "undefined" : value;
const error = new Error(
`No more mocked responses for the query: ${print(
operation.query
)}, variables: ${JSON.stringify(operation.variables)}`
));
)}\nExpected variables:\n\t${JSON.stringify(operation.variables, replacer)}${
diffs.length > 0
? `\nFound ${diffs.length} mock${
diffs.length > 1 ? "s" : ""
} with variables:\n${diffs.map(
(d, i) => `\t${i + 1}: ${JSON.stringify(d, replacer)}\n`
)}`
: ""
}`
)
this.onError(error);
return null;
}

Expand Down Expand Up @@ -141,7 +154,7 @@ export class MockLink extends ApolloLink {
): MockedResponse {
const newMockedResponse = cloneDeep(mockedResponse);
const queryWithoutConnection = removeConnectionDirectiveFromDocument(
newMockedResponse.request.query
newMockedResponse.request.query
);
invariant(queryWithoutConnection, "query is required");
newMockedResponse.request.query = queryWithoutConnection!;
Expand Down