{"id":22540597,"url":"https://github.com/batrdn/nock-graphql","last_synced_at":"2025-08-04T05:31:51.931Z","repository":{"id":41147892,"uuid":"357044371","full_name":"batrdn/nock-graphql","owner":"batrdn","description":"Minimal client-side GraphQL testing tool","archived":true,"fork":false,"pushed_at":"2024-03-03T14:51:51.000Z","size":287,"stargazers_count":18,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-06T10:07:10.845Z","etag":null,"topics":["graphql","graphql-mock","graphql-testing","nock","nock-graphql","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/batrdn.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-12T03:23:32.000Z","updated_at":"2024-12-06T19:40:23.000Z","dependencies_parsed_at":"2024-10-30T21:01:20.637Z","dependency_job_id":"82830e38-5628-42eb-8a80-d2cda5a9ed05","html_url":"https://github.com/batrdn/nock-graphql","commit_stats":{"total_commits":25,"total_committers":2,"mean_commits":12.5,"dds":0.07999999999999996,"last_synced_commit":"540ba7389c30a6d6cfd89aa613a05d88e78611ea"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/batrdn/nock-graphql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/batrdn%2Fnock-graphql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/batrdn%2Fnock-graphql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/batrdn%2Fnock-graphql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/batrdn%2Fnock-graphql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/batrdn","download_url":"https://codeload.github.com/batrdn/nock-graphql/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/batrdn%2Fnock-graphql/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268654311,"owners_count":24285120,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-04T02:00:09.867Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","graphql-mock","graphql-testing","nock","nock-graphql","unit-testing"],"created_at":"2024-12-07T12:11:32.088Z","updated_at":"2025-08-04T05:31:51.603Z","avatar_url":"https://github.com/batrdn.png","language":"TypeScript","readme":"# nock-graphql\n\nA nock-based GraphQL testing library that provides a functionality to mock queries and mutations.\nIn contrast to Apollo's MockedProvider, nock allows a realistic testing with the actual `http` calls being made from your client code.\n\n## Installing\n\nYou'll need `nock`, `cross-fetch` as peer dependencies in order to use this library.\n\n```\nnpm install -D nock-graphql nock cross-fetch\n```\n\nAdditionally, you need to set up global `fetch` implementation incorporated in your `jest` environment:\n\n```\nglobal.fetch = require('cross-fetch')\n```\n\nor, directly in your `ApolloClient`\n\n```react\nconst client = new ApolloClient({\n  link: new HttpLink({ fetch, uri: ENDPOINT }),\n});\n```\n\n## Usage\n\nI. Create `nock-graphql` instance\n\nA. You could create it globally:\n\n```typescript\n// Create the instance in a separate file and export it.\nimport { NockGraphQL } from 'nock-graphql';\n\nexport const nockgql = new NockGraphQL('http://localhost:4000/graphql');\n```\n\nB. Create directly in the test files:\n\n```typescript\nimport { NockGraphQL } from 'nock-graphql';\n\nlet nockgql: NockGraphQL;\n\nbeforeAll(() =\u003e {\n  nockgql = new NockGraphQL('http://localhost:4000/graphql');\n});\n```\n\nII. Mocking queries\n\n```typescript\nimport { gql } from 'graphql-tag';\nimport { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client';\nimport { MockConfig } from 'nock-graphql';\n\ntype QueryVariables = {\n  id: string;\n};\n\ntype QueryResult = {\n  foo: { bar: string };\n};\n\nconst GetQuery = gql`\n  query Test($id: String) {\n    foo(id: $id) {\n      bar\n    }\n  }\n`;\n\nbeforeEach(() =\u003e {\n  client = new ApolloClient({\n    link: new HttpLink({ uri: 'http://localhost:4000/graphql' }),\n    cache: new InMemoryCache({ addTypename: false }),\n  });\n});\n\nafterEach(() =\u003e {\n  nockgql.cleanup();\n});\n\ntest('should match  the query', async () =\u003e {\n  const config: MockConfig\u003cQueryVariables, QueryResult\u003e = {\n    document: GetQuery,\n    variables: { id: '1' },\n    data: {\n      foo: {\n        bar: 'Hello, World',\n      },\n    },\n  };\n\n  const scope = nockgql.mock(config);\n  await client.query({ query: GetQuery, variables: { id: '1' } });\n\n  scope.done();\n});\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbatrdn%2Fnock-graphql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbatrdn%2Fnock-graphql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbatrdn%2Fnock-graphql/lists"}