@shopify/graphql-testing
Jest + Enzyme mocks for Apollo 2.x.
$ yarn add @shopify/jest-mock-apollo
This package provides a decorator for building mock GraphQL clients specific to your project. The initial function call takes in project-wide configuration and emits a customized decorator to be used across your project. For example:
// tests/utilities/graphql.ts
import {buildSchema} from 'graphql';
import configureClient from '@shopify/jest-mock-apollo';
// You likely want to import this from somewhere in real production code
const schemaSrc = `
type Product {
id: Int!
title: String
}
type Query {
products: [Product]
}
`;
const schema = buildSchema(schemaSrc);
const createGraphQLClient = configureClient({schema});
export {createGraphQLClient};
The factory requires a GraphQLSchema
(schema
) in order to ensure that more useful error messages are shown and that mocks are validated against the schema.
Take a look at the test suite for examples on how to use enzyme's mock context.
See the comments and TypeScript annotations in the code for details on the provided utilities.