Registry / testing / graphql-mock

graphql-mock

JSON →
library2.0.0jsnpmunverified

graphql-mock is a declarative mocking library for Apollo GraphQL client testing, inspired by nock/sinon. Version 2.0.0 (latest) provides ESM with TypeScript types. It enables developers to specify exact response data, test failure and loading states, and assert queries/mutations sent by code. Differentiators include seamless integration with react-apollo and react-apollo-hooks, automatic client refresh on reset, and support for variables assertion. Released under MIT license by MadRabbit.

npm install graphql-mock
INSTALL
IMPORT
SIG · GRAPHQL-MOCK
G
graphql-mock
testingjavascriptv2.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default export or named?
import { graphqlMock } from 'graphql-mock'
import graphqlMock from 'graphql-mock'
Package exports a named export, not a default. Use named import.
graphqlMock.client
const { client } = graphqlMock
const client = require('graphql-mock').client
ESM-only; after each reset, client is auto-renewed. Do not destructure outside render cycle.
graphqlMock.expect(query)
graphqlMock.expect(query).reply({...})
graphqlMock.expect(query, {...})
Use .reply() chain, not arguments to expect.

Shows basic mocking of a GraphQL query with success, failure, and loading states using Enzyme and ApolloProvider.

import { mount } from 'enzyme'; import { ApolloProvider } from 'react-apollo'; import { graphqlMock } from 'graphql-mock'; import { TodoList } from './components'; const query = `query Todos { items { id name } }`; const render = () => mount( <ApolloProvider client={graphqlMock.client}> <TodoList /> </ApolloProvider> ); describe('<TodoList />', () => { it('renders server data', () => { graphqlMock.expect(query).reply({ data: { items: [{ id: '1', name: 'one' }, { id: '2', name: 'two' }] } }); expect(render().html()).toEqual('<ul><li>one</li><li>two</li></ul>'); }); it('handles failure', () => { graphqlMock.expect(query).fail('everything is terrible'); expect(render().html()).toEqual('<div>everything is terrible</div>'); }); it('shows loading', () => { graphqlMock.expect(query).loading(true); expect(render().html()).toEqual('<div>Loading...</div>'); }); });
Debug
Known issues
gotchareact-apollo-hooks requires a new client per render; after reset client is auto-renewed but destructuring client outside render cycle reuses stale instance.
fix
Always reference graphqlMock.client inside the render function, not outside.
affects: *
gotchaMutations with react-apollo-hooks wrap responses in extra promises, requiring manual wait and update.
fix
After simulate, add 'await new Promise(r => setTimeout(r, 10)); wrapper.update();' before assertions.
affects: *
breakingPackage is ESM-only since v2.0.0; require() will throw.
fix
Use import syntax; switch to ESM module configuration.
affects: >=2.0.0
deprecatedOlder versions (pre-2.0) had a different API (e.g., mock.expect({}) instead of mock.expect(query).reply()).
fix
Upgrade to v2.0.0 and update calls to use .reply(), .fail(), .loading() chains.
affects: <2.0.0
Errors
Common errors & fixes
Unexpected token: 'export'
Using CommonJS require() with an ESM-only module.
fix
Use import { graphqlMock } from 'graphql-mock' or enable ESM in your project.
TypeError: graphqlMock.expect is not a function
Importing default instead of named export.
fix
Use import { graphqlMock } from 'graphql-mock' instead of default import.
Cannot read properties of undefined (reading 'client')
Destructuring client outside render cycle; client may be null or stale.
fix
Reference graphqlMock.client directly inside the render function, not via destructured variable.
Expected request body does not match mock
Query string mismatch (whitespace, formatting) or variables not passed correctly.
fix
Ensure query strings are identical (use template literals) and include variables if needed.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
apollo-clientoptionalPeer dependency for mocking Apollo Client
graphql-tagrequiredFor parsing GraphQL queries in mock definitions
Agent activity
6 hits · last 30 days
node
6
Resources
graphql-mock — npm install graphql-mock · libregistry