-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Mocking UseLazyQuery does not return data #6865
Comments
Your mocking seems to be incorrect, the above test succeeds for me after changing the result mock to
See this comment |
Looks like you are right! I see now, will have to take another look at my original implementation. (Was expecting it to return what ever I set data to) Thanks! |
If you wanna mock using jest, you use the following approach
As you see, this approach even returns the mock function that is called in the code to be tested. |
I am seeing the same problem with @apollo/client v3.3.20 when using |
@onichandame I'd agree, I'm also seeing this same issue with useLazyQuery. |
I'm also having this problem. I cannot get respond data from 'onCompleted' in useLazyQuery(By MockedProvider). import { DocumentNode } from 'graphql'
import * as Apollo from '@apollo/client'
import { LazyQueryHookOptions, NetworkStatus, QueryLazyOptions, QueryTuple, TypedDocumentNode } from '@apollo/client'
export const makeMockedUseLazyQuery = () => Record<string, unknown>) => {
const mockedUseLazyQuery = jest.spyOn(Apollo, 'useLazyQuery')
mockedUseLazyQuery.mockImplementation((query: DocumentNode | TypedDocumentNode<unknown, unknown>,
options?: LazyQueryHookOptions<unknown, unknown>): QueryTuple<unknown, unknown> => {
return [
jest.fn().mockImplementation((_?: QueryLazyOptions<any>) => {
options?.onCompleted?.({
users: {
id: 1,
name: "mocked!",
...
}
})
}),
{
loading: false,
networkStatus: NetworkStatus.ready,
called: false,
data: undefined,
},
]
})
return mockedUseLazyQuery
} You can make conditional return data(by switch or if statement) in function's body. |
@onichandame If your tests are not working correctly, that means your mocked responses that you feed into MockedProvider are incorrect. |
@vipul0092 do you have a resource that shows examples of a useLazyQuery response? The Apollo docs are somewhat lacking in this area (for useLazyQuery anyways) In my case I'm using both in various parts of code, but the useQuery has no issues when the MockedProvider attempts to return the data. Given that I created the return data object the same way for both useQuery and useLazyQuery (even went as far as to copy + paste the version from useQuery that was working to double check myself) I would expect to have no issues. At the very least it would be helpful for the mocked provider to throw warnings if it's unable to respond to a request based on return data formatting. |
Hey Guys! It solved when using useLazyQuery in MockedProvider! I'm using @apollo/client '3.5.5'. And the reason MockedProvider did not work on useLazyQuery was mismatching: 'variables', 'data'. In other words, (variables & data) of mocks should be same with (variables & data) of useLazyQuery in real component that you want to test. For example, // In real Component,
const [myLazyQuery, myLazyQueryOptions] = useLazyQuery<QueryDataType, QueryVariablesType>(MY_QUERY, {
variables: {
....
},
onCompleted: () => { ... }
...
}) & // In test,
const mocks = [
{
request: {
query: MY_QUERY
variables: QueryVariablesType // It should be same!!!
},
result: {
data: QueryDataType // It should be same!!!
}
}
]
...
// You also should set 'false' addTypeName.
// If set 'true', you should declare '__typename' in each mocks data.
<MockedProvider mocks={mocks} addTypeName={false}>
<YourComponent../>
</MockedProvider> So I recommend that using with type that declare Mock's type. Like this, export type MockType<TData, TVariables> = {
request: {
query: DocumentNode
variables: TVariables
},
result: {
data: TData
}
}
...
const myMocks: MockType<QueryDataType, QueryVariablesType>[] = [{ ... }, { ... }] I hope this would help. |
Full example please! |
Here is an example of a component that uses
|
not working for me; not sure because I have 2 consequence useLazyQuery? second one depends on first one; but both return null; I use apollo client 3.5.8; thanks |
MockedProvider is a real joke really, can't count the time I lost debugging this crap |
Actually MockedProvider is working, the tricky is you need "Your test must execute an operation that exactly matches a mock's shape and variables to receive the associated mocked response.", I guess this is in the document; So that means when you define your Apollo client query, make sure you have defined type for response data. |
Try adding a console.log inside your onError block in the real component. It will notify you what your shape of the object is missing. |
Intended outcome:
After setting up MockedProvider with query mocks, uselazyquery hook should return mocked data after loading state.
Actual outcome:
Data is undefined, and no query results are returned
How to reproduce the issue:
Versions
npx: installed 1 in 1.939s
System:
OS: macOS Mojave 10.14.6
Binaries:
Node: 12.11.1 - ~/.nvm/versions/node/v12.11.1/bin/node
Yarn: 1.22.4 - ~/code/project-mercury/node_modules/.bin/yarn
npm: 6.11.3 - ~/.nvm/versions/node/v12.11.1/bin/npm
Browsers:
Chrome: 84.0.4147.125
Firefox: 77.0.1
Safari: 13.1.2
npmPackages:
@apollo/client: ^3.1.3 => 3.1.3
@apollo/react-hooks: ^4.0.0 => 4.0.0
@apollo/react-testing: ^4.0.0 => 4.0.0
apollo-boost: ^0.4.7 => 0.4.7
apollo-cache-inmemory: ^1.6.5 => 1.6.5
Other Mentions
I believe these issues were related
apollographql/react-apollo#3899
https://spectrum.chat/apollo/testing/how-to-test-uselazyquery~c3a55a74-2a15-4c6d-a0f7-e11966c02479
Hacky Workaround I was playing with
The text was updated successfully, but these errors were encountered: