{"id":47908,"library":"graphql-mock","title":"graphql-mock","description":"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.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/MadRabbit/graphql-mock","tags":["javascript","typescript"],"install":[{"cmd":"npm install graphql-mock","lang":"bash","label":"npm"},{"cmd":"yarn add graphql-mock","lang":"bash","label":"yarn"},{"cmd":"pnpm add graphql-mock","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency for mocking Apollo Client","package":"apollo-client","optional":true},{"reason":"For parsing GraphQL queries in mock definitions","package":"graphql-tag","optional":false}],"imports":[{"note":"Package exports a named export, not a default. Use named import.","wrong":"import graphqlMock from 'graphql-mock'","symbol":"default export or named?","correct":"import { graphqlMock } from 'graphql-mock'"},{"note":"ESM-only; after each reset, client is auto-renewed. Do not destructure outside render cycle.","wrong":"const client = require('graphql-mock').client","symbol":"graphqlMock.client","correct":"const { client } = graphqlMock"},{"note":"Use .reply() chain, not arguments to expect.","wrong":"graphqlMock.expect(query, {...})","symbol":"graphqlMock.expect(query)","correct":"graphqlMock.expect(query).reply({...})"}],"quickstart":{"code":"import { mount } from 'enzyme';\nimport { ApolloProvider } from 'react-apollo';\nimport { graphqlMock } from 'graphql-mock';\nimport { TodoList } from './components';\n\nconst query = `query Todos { items { id name } }`;\n\nconst render = () => mount(\n  <ApolloProvider client={graphqlMock.client}>\n    <TodoList />\n  </ApolloProvider>\n);\n\ndescribe('<TodoList />', () => {\n  it('renders server data', () => {\n    graphqlMock.expect(query).reply({\n      data: { items: [{ id: '1', name: 'one' }, { id: '2', name: 'two' }] }\n    });\n    expect(render().html()).toEqual('<ul><li>one</li><li>two</li></ul>');\n  });\n\n  it('handles failure', () => {\n    graphqlMock.expect(query).fail('everything is terrible');\n    expect(render().html()).toEqual('<div>everything is terrible</div>');\n  });\n\n  it('shows loading', () => {\n    graphqlMock.expect(query).loading(true);\n    expect(render().html()).toEqual('<div>Loading...</div>');\n  });\n});","lang":"typescript","description":"Shows basic mocking of a GraphQL query with success, failure, and loading states using Enzyme and ApolloProvider."},"warnings":[{"fix":"Always reference graphqlMock.client inside the render function, not outside.","message":"react-apollo-hooks requires a new client per render; after reset client is auto-renewed but destructuring client outside render cycle reuses stale instance.","severity":"gotcha","affected_versions":"*"},{"fix":"After simulate, add 'await new Promise(r => setTimeout(r, 10)); wrapper.update();' before assertions.","message":"Mutations with react-apollo-hooks wrap responses in extra promises, requiring manual wait and update.","severity":"gotcha","affected_versions":"*"},{"fix":"Use import syntax; switch to ESM module configuration.","message":"Package is ESM-only since v2.0.0; require() will throw.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Upgrade to v2.0.0 and update calls to use .reply(), .fail(), .loading() chains.","message":"Older versions (pre-2.0) had a different API (e.g., mock.expect({}) instead of mock.expect(query).reply()).","severity":"deprecated","affected_versions":"<2.0.0"}],"env_vars":null,"search_vec":"'2.0.0':21 'apollo':13,54,58 'assert':42,69 'automat':60 'client':15,61 'code':46 'data':35 'declar':9 'develop':30 'differenti':47 'enabl':29 'esm':24 'exact':33 'failur':37 'graphql':2,5,14 'graphql-mock':1,4 'hook':59 'includ':48 'inspir':17 'integr':50 'javascript':76 'latest':22 'librari':11 'licens':73 'load':39 'madrabbit':75 'mit':72 'mock':3,6,10 'nock/sinon':19 'provid':23 'queries/mutations':43 'react':53,57 'react-apollo':52 'react-apollo-hook':56 'refresh':62 'releas':70 'reset':64 'respons':34 'seamless':49 'sent':44 'specifi':32 'state':40 'support':66 'test':16,36 'type':27 'typescript':26,77 'variabl':68 'version':20","created_at":"2026-06-07T16:53:57.370750+00:00","updated_at":"2026-06-07T16:53:57.370750+00:00","problems":[{"fix":"Use import { graphqlMock } from 'graphql-mock' or enable ESM in your project.","cause":"Using CommonJS require() with an ESM-only module.","error":"Unexpected token: 'export'"},{"fix":"Use import { graphqlMock } from 'graphql-mock' instead of default import.","cause":"Importing default instead of named export.","error":"TypeError: graphqlMock.expect is not a function"},{"fix":"Reference graphqlMock.client directly inside the render function, not via destructured variable.","cause":"Destructuring client outside render cycle; client may be null or stale.","error":"Cannot read properties of undefined (reading 'client')"},{"fix":"Ensure query strings are identical (use template literals) and include variables if needed.","cause":"Query string mismatch (whitespace, formatting) or variables not passed correctly.","error":"Expected request body does not match mock"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/MadRabbit/graphql-mock#readme","github":"ssh://git@github.com/MadRabbit/graphql-mock","docs":null,"changelog":null,"pypi":null,"npm":"graphql-mock","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}