facebook / facebook/relay

Unable to mock fetchQuery in Relay for testing

Open
#4,283 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

**Title**: Unable to mock fetchQuery in Relay for testing

**Issue description:**

I am currently working on a Relay project and I need to test a component that uses `fetchQuery` to execute a query. However, I am unable to find a straightforward way to mock the response for fetchQuery. I am aware of the `relay-test-utils` package and its `createMockEnvironment` function, but I am not sure how to apply it specifically for `fetchQuery`.

Here's an example of the code I am testing:

```
export const getLocationName = (
LocationFilter: LocationQuery$variables
) => {
return new Promise((resolve) => {
fetchQuery(
RelayEnvironment,
LocationQuery,
LocationFilter,
{ fetchPolicy: "network-only" }
).subscribe({
next: (data) => {
if (data.eLocation?.edges) {
resolve(convertData(data.Location.edges));
}
resolve([]);
},
});
});
};
```
I have tried using createMockEnvironment and tried various approaches to mock the response of `fetchQuery`, but I have not been successful.

Could you please guide me on how to properly mock the response of `fetchQuery` for testing purposes? Any help or pointers would be greatly appreciated.

Thank you.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.