⭐️ graphql-automock ⭐️
Automatically mock GraphQL schemas for better testing.
Features:
- Automatically and deterministically mock GraphQL schemas
- Mock
react-apollo
for simple UI testing - Control schema execution to reliably test loading and success states
Getting started
Install via npm or yarn:
npm install --save-dev graphql-automockyarn add --dev graphql-automock
Mocking just the schema
Simply pass your GraphQL type definitions to mockSchema
and
you're ready to go:
;; const types = ` type Query { recentPosts: [Post!]! } type Post { id: ID! content: String! likes: Int! }`; const mocked = ; const query = `{ recentPosts { id content likes }}`; ;
Without any further configuration, this query will return:
To understand how these values are derived, see Default values.
Mocking react-apollo
In addition to schema mocking, <MockApolloProvider>
makes the testing of your UI components much easier.
Wrapping components in a <MockApolloProvider>
allows any graphql()
and <Query>
components in the tree to receive mock data.
However note that components will first enter a loading state before the query resolves and components re-render.
<MockApolloProvider>
, together with a controller
, allows you to step through GraphQL execution to test both loading and ready states.
;; ;
Customizing mocks
Automatically mocking the entire schema with sensible, deterministic data allows test code to customize only the data that affects the test. This results in test code that is more concise and easier to understand:
// We're only interested in the behaviour of likes...;
Mocking errors
Both GraphQL errors and network errors can be mocked.
Mocking GraphQL errors
Just like with a real GraphQL implementation, GraphQL errors are generated by throwing an error from a (mock) resolver.
; ;
Mocking network errors
Since network errors are external to the GraphQL schema, they are simulated through the controller
.
;; ;
Default values
To ensure that tests are reliable, the values generated by graphql-automock are 100% deterministic. The following default values are used:
- Boolean:
true
- Int:
2
- Float:
3.14
- String: Path to value
- ID: Path to value
- Enum: The first enum value, sorted alphabetically by name
- Interface: The first possible implementation, sorted alphabetically by name
- Union: The first possible member type, sorted alphabetically by name
- List length:
2
API Reference
mockSchema()
Create a mocked GraphQL schema.
: GraphQLSchema; function mockSchema({ schema: String | GraphQLSchema mocks: String: MockResolverFn }): GraphQLSchema;
mockApolloClient()
Create a mocked Apollo Client.
: ApolloClient; function mockApolloClient({ schema: String | GraphQLSchema mocks: String: MockResolverFn controller: Controller}): ApolloClient;
<MockApolloProvider>
React component that renders a mocked ApolloProvider.
<MockApolloProvider schema=String | GraphQLSchema mocks= String: MockResolverFn controller=Controller>
type MockResolverFn
type any;
controller
Gives precise control over GraphQL execution, as well as enabling network errors to be simulated.
pause()
: void;
Pause GraphQL execution until it is explicitly resumed.
Controller starts in this state.
run()
: Promise<void>;function run({ any }): Promise<void>;
Resume GraphQL execution if it is paused.
Returns a Promise that resolves when all pending queries have finished executing. If execution was not paused, then it returns a resolved Promise.
If a networkError
function is provided, pending and subsequent queries will fail with the result of calling that function. The function is called once for each query.