Testing
Apollo Client React testing API
MockedProvider
MockedProvider
import { MockedProvider } from "@apollo/client/testing";
The MockedProvider
component is a mocked version of ApolloProvider
Props
Name / Type | Description |
---|---|
mocks
| An array containing GraphQL operation definitions and their corresponding mocked responses. See |
addTypename
| If The default value is |
defaultOptions
| An object containing options to pass directly to the |
cache
| A custom cache for the By default, |
resolvers
| Deprecated. A collection of |
childProps
| Props to pass down to the |
showWarnings
| When a request fails to match a mock, a warning is logged to the console to indicate the mismatch. Set this to The default value is |
Example mocks
array
mocks
arrayconst 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
.