Registry / testing / graphql-query-test-mock

graphql-query-test-mock

JSON →
library0.12.1jsnpmunverified

A library to mock GraphQL queries when testing clients making calls to GraphQL APIs. Ideal for use with Jest and Relay Modern/Apollo, but not bound to any specific client library. Focus is on realistic testing using nock to intercept HTTP requests. Current stable version: 0.12.1. Release cadence is irregular. Key differentiators: variable matching to verify correctness, control over query resolution timing for loading states, ability to simulate HTTP error statuses, and support for sequences of responses for pagination scenarios.

npm install graphql-query-test-mock
INSTALL
IMPORT
SIG · GRAPHQL-QUERY-TEST
G
graphql-query-test-mock
testingjavascriptv0.12.1
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.

QueryMock
import { QueryMock } from 'graphql-query-test-mock'
import QueryMock from 'graphql-query-test-mock'
QueryMock is a named export, not default. Common mistake: using default import.
QueryMock
const { QueryMock } = require('graphql-query-test-mock')
const QueryMock = require('graphql-query-test-mock')
For CommonJS, destructure the require. Wrong: assigning the whole module to QueryMock.
GraphQLMock
import { GraphQLMock } from 'graphql-query-test-mock'
import { GraphQL } from 'graphql-query-test-mock'
If using TypeScript, GraphQLMock type is exported. Wrong: mistyping export name.

Demonstrates setup, teardown, and basic query mocking with Jest. Includes mocking a query and resolving all pending queries.

import { QueryMock } from 'graphql-query-test-mock'; const queryMock = new QueryMock(); beforeEach(() => { queryMock.setup('https://api.example.com/graphql'); }); afterEach(() => { queryMock.reset(); }); test('should mock a query', async () => { queryMock.mockQuery({ name: 'UserQuery', data: { user: { id: '1', name: 'John' } } }); // Your test logic that triggers the query // ... await queryMock.resolveAll(); expect(someElement).toHaveTextContent('John'); });
Debug
Known issues
gotchaRequires a fetch implementation globally (e.g., node-fetch) in the test environment, as Jest does not provide one by default.
fix
Add 'global.fetch = require('node-fetch');' in your Jest setup file.
affects: >=0.0.0
gotchaThe mock URL passed to setup() must exactly match the GraphQL endpoint used in your client code, including protocol and path.
fix
Ensure the URL in setup() matches the one in your client code exactly.
affects: >=0.0.0
breakingIn version 0.12.0, the import path changed from 'graphql-query-test-mock/lib/QueryMock' to 'graphql-query-test-mock'.
fix
Use 'import { QueryMock } from 'graphql-query-test-mock'; instead of the old path.
affects: >=0.12.0
Errors
Common errors & fixes
TypeError: Cannot read property 'setup' of undefined
QueryMock was imported as default instead of named import.
fix
Use import { QueryMock } from 'graphql-query-test-mock';
Nock: No match for request ...
The URL provided to queryMock.setup() does not match the URL being called by your client code.
fix
Verify that the URL passed to setup() matches exactly the GraphQL endpoint your client uses.
fetch is not defined
The test environment lacks a global fetch implementation.
fix
Add global.fetch = require('node-fetch'); in your Jest setup file.
Upgrade
Version history
0.12.1latest on npm
Audit
Dependencies
nockrequiredPeer dependency. nock is used to intercept HTTP requests at the http level to simulate GraphQL API responses.
fetchoptionalRuntime dependency. A fetch implementation (e.g., node-fetch) must be globally available in the test environment, as Jest does not include one by default.
Agent activity
6 hits · last 30 days
node
6
Resources
graphql-query-test-mock — npm install graphql-query-test-mock · libregistry