Registry / testing / nock-graphql

nock-graphql

JSON →
library2.1.0jsnpmunverified

Nock-based GraphQL testing library (v2.1.0) that mocks GraphQL queries and mutations by intercepting actual HTTP calls via nock. Unlike Apollo's MockedProvider, it tests with real fetch requests, providing more realistic integration tests. Ships TypeScript types. Requires nock, cross-fetch, and graphql as peer dependencies. Suitable for Apollo Client and other GraphQL clients that use HTTP.

npm install nock-graphql
INSTALL
IMPORT
SIG · NOCK-GRAPHQL
N
nock-graphql
testingjavascriptv2.1.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.

NockGraphQL
import { NockGraphQL } from 'nock-graphql'
const nockGraphQL = require('nock-graphql')
ESM only; package ships TypeScript types.
MockConfig
import { MockConfig } from 'nock-graphql'
TypeScript type for configuration object, not a runtime export.

Shows how to instantiate NockGraphQL, mock a GraphQL query, execute it with Apollo Client, and verify the mock was called.

import { NockGraphQL } from 'nock-graphql'; import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client'; import fetch from 'cross-fetch'; import { gql } from 'graphql-tag'; const client = new ApolloClient({ link: new HttpLink({ uri: 'http://localhost:4000/graphql', fetch }), cache: new InMemoryCache({ addTypename: false }), }); const nockgql = new NockGraphQL('http://localhost:4000/graphql'); const query = gql`query GetUser($id: ID!) { user(id: $id) { name } }`; const scope = nockgql.mock({ document: query, variables: { id: '1' }, data: { user: { name: 'Alice' } }, }); await client.query({ query, variables: { id: '1' } }); scope.done(); nockgql.cleanup();
Debug
Known issues
breakingNode.js >=18 and npm >=9 required.
fix
Upgrade Node.js to 18+ and npm to 9+.
affects: >=2.0.0
gotchaglobal.fetch must be polyfilled (e.g., cross-fetch) in Jest environment, otherwise tests fail with fetch is not defined.
fix
Add global.fetch = require('cross-fetch') in test setup file.
affects: all
gotchaAfter each test, call nockgql.cleanup() to avoid cross-test pollution.
fix
Use afterEach to call cleanup().
affects: all
deprecatedIf using Apollo Client with addTypename: true (default), you must include __typename in mock data or mock will not match.
fix
Set addTypename: false in InMemoryCache or include __typename fields.
affects: all
Errors
Common errors & fixes
fetch is not defined
cross-fetch not polyfilled in test environment
fix
global.fetch = require('cross-fetch')
Nock: No match for request
Variables or query document mismatch between nock mock and actual request
fix
Ensure variables match exactly (including order and types) and document string is identical.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
cross-fetchrequiredRequired to polyfill fetch for Node.js testing environment
graphqlrequiredPeer dependency for parsing GraphQL documents
nockrequiredPeer dependency for HTTP mocking
Agent activity
4 hits · last 30 days
node
4
Resources
nock-graphql — npm install nock-graphql · libregistry