Testing
Apollo Client React testing API
For more guidance on running tests with
MockedProvider
, see Testing React components.
MockedProvider
import { MockedProvider } from "@apollo/client/testing";
The MockedProvider
component is a mocked version of ApolloProvider
that doesn't send network requests to your API. Instead, you to specify the exact response payload for a given GraphQL operation. This enables you to test your application's operations without communicating with a server.
Props
Name / Type |
Description |
---|---|
|
An array containing GraphQL operation definitions and their corresponding mocked responses. See Defining mocked responses. |
|
If The default value is |
|
An object containing options to pass directly to the |
|
A custom cache for the By default, |
|
Deprecated. A collection of local resolvers for the |
|
Props to pass down to the |
Example mocks
array
const mocks = [
{
request: {
query: GET_DOG,
variables: { index: 4 }
},
result: {
data: {
dog: {
name: "Douglas"
}
}
}
},
{
request: {
query: GET_DOG,
variables: { index: 8 }
},
error: new Error("Something went wrong")
}
]
With the mocks
array above:
- If the
GET_DOG
operation is executed with variables{ index: 4 }
, it returns a dog namedDouglas
. - If
GET_DOG
is executed with variables{ index: 8 }
, it returns anerror
.