-
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
Random testing results when using useLazyQuery and useEffect #7388
Comments
@cjduncana Thanks for the reproduction! We'll take a look and get back to you soon, though don't hesitate to @-mention me if it's been more than a week. |
@benjamn Any news? |
@benjamn Any updates on this ? |
Sorry about the delayed response. So the problem with the test is that you’re waiting for a promise based on setTimeout. There is no guarantee that the useEffect calls in your component will have been called by the time the test promise resolves. it('should render component', async () => {
const mock = {
request: { query: ALL_PEOPLE },
result: {
data: { people: [{ id: 1, name: 'John Doe' }] },
},
};
const component = TestRenderer.create(
<MockedProvider mocks={[mock]} addTypename={false}>
<App />
</MockedProvider>,
);
await TestRenderer.waitFor(() => {
const p = component.root.findByType('li');
expect(p.props.children).toEqual('John Doe');
});
}); This doesn’t seem like something we can fix in Apollo Client itself, so I’m closing the issue. Please ping and yell at me if this is not the case. 😇 |
Context:
I have a value that may exist when the page opens. If there's a value, I want to run the query, if the value changes, I want to rerun the query with the new value, and if there's no value, the code should not run the query. To solve this, I placed the trigger to the lazy query inside a useEffect that conditionally runs or reruns the query based on the existence or change of the value. When I went to test this component, that's when I discovered this bug. Using useQuery does not trigger this behavior.
Example of my code:
Further down, I included a minimal, reproducible example using the provided React Apollo error template.
Intended outcome:
Running a test should have consistent results
Actual outcome:
The test will fail randomly. When the test fails, the useLazyQuery return these results:
When the test succeeds, the useLazyQuery return these results:
How to reproduce the issue:
Minimal, Reproducible Example
Use useLazyQuery and place its trigger inside of a useEffect, then test the query following the instructions in the Apollo docs.
Versions
System:
OS: macOS 10.15.7
Binaries:
Node: 12.19.0 - ~/.nvm/versions/node/v12.19.0/bin/node
npm: 6.14.8 - ~/.nvm/versions/node/v12.19.0/bin/npm
Browsers:
Safari: 14.0.1
npmPackages:
@apollo/client: ^3.2.5 => 3.2.5
The text was updated successfully, but these errors were encountered: