{"id":21840313,"url":"https://github.com/zapier/apollo-server-integration-testing","last_synced_at":"2025-04-06T04:13:28.991Z","repository":{"id":35055927,"uuid":"201443979","full_name":"zapier/apollo-server-integration-testing","owner":"zapier","description":"Test helper for writing apollo-server integration tests","archived":false,"fork":false,"pushed_at":"2023-04-17T11:53:13.000Z","size":167,"stargazers_count":134,"open_issues_count":6,"forks_count":21,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-04-14T04:45:32.988Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/zapier.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":"2019-08-09T10:13:51.000Z","updated_at":"2024-06-18T15:27:25.021Z","dependencies_parsed_at":"2024-06-18T15:27:16.653Z","dependency_job_id":"727b4cc9-f015-42c2-ab1b-0f4d93a0ab59","html_url":"https://github.com/zapier/apollo-server-integration-testing","commit_stats":{"total_commits":39,"total_committers":10,"mean_commits":3.9,"dds":0.4358974358974359,"last_synced_commit":"7403df8019af9cb28dd04a67af40725037bfdc55"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zapier%2Fapollo-server-integration-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zapier%2Fapollo-server-integration-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zapier%2Fapollo-server-integration-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zapier%2Fapollo-server-integration-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zapier","download_url":"https://codeload.github.com/zapier/apollo-server-integration-testing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247430878,"owners_count":20937874,"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","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":[],"created_at":"2024-11-27T21:25:36.681Z","updated_at":"2025-04-06T04:13:28.893Z","avatar_url":"https://github.com/zapier.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# apollo-server-integration-testing\n\nThis package exports an utility function for writing apollo-server integration tests:\n\n```\nimport { createTestClient } from 'apollo-server-integration-testing';\n```\n\n## Usage\n\nThis function takes in an apollo server instance and returns a function that you can use to run operations against your schema, and assert on the results.\n\nExample usage:\n\n```js\nimport { createTestClient } from 'apollo-server-integration-testing';\nimport { createApolloServer } from './myServerCreationCode';\n\nconst apolloServer = await createApolloServer();\nawait apolloServer.start();\nconst { query, mutate } = createTestClient({\n  apolloServer,\n});\n\nconst result = await query(`{ currentUser { id } }`);\n\nexpect(result).toEqual({\n  data: {\n    currentUser: {\n      id: '1',\n    },\n  },\n});\n\nconst UPDATE_USER = `\n  mutation UpdateUser($id: ID!, $email: String!) {\n    updateUser(id: $id, email: $email) {\n      user {\n        email\n      }\n    }\n  }\n`;\n\nconst mutationResult = await mutate(UPDATE_USER, {\n  variables: { id: 1, email: 'nancy@foo.co' },\n});\n\nexpect(mutationResult).toEqual({\n  data: {\n    updateUser: {\n      email: 'nancy@foo.co',\n    },\n  },\n});\n```\n\nThis allows you to test all the logic of your apollo server, including any logic inside of the `context` option that you can pass to the `ApolloServer` constructor.\n\n### Mocking the `Request` or `Response` object\n\n`createTestClient` automatically mocks the `Request` and `Response` objects that will be passed to the `context` option of your `ApolloServer` constructor, so testing works out of the box.\nYou can also extend the mocked Request or Response object with additional keys by passing an `extendMockRequest` or `extendMockResponse` field to `createTestClient`:\n\n```js\nconst { query } = createTestClient({\n  apolloServer,\n  extendMockRequest: {\n    headers: {\n      cookie: 'csrf=blablabla',\n      referer: '',\n    },\n  },\n  extendMockResponse: {\n    locals: {\n      user: {\n        isAuthenticated: false,\n      },\n    },\n  },\n});\n```\n\nThis is useful when your apollo server `context` option is a callback that operates on the passed in `req` key, and you want to inject data into that `req` object.\n\nAs mentioned above, if you don't pass an `extendMockRequest` to `createTestClient`, we provide a default request mock object for you. See https://github.com/howardabrams/node-mocks-http#createrequest for all the default values that are included in that mock.\n\n### setOptions\n\nYou can also set the `request` and `response` mocking options **after** the creation of the `test client`, which is a **cleaner** and **faster** way due not needing to create a new instance for **any** change you might want to do the `request` or `response`.\n\n```js\nconst { query, setOptions } = createTestClient({\n  apolloServer,\n});\n\nsetOptions({\n  // If \"request\" or \"response\" is not specified, it's not modified\n  request: {\n    headers: {\n      cookie: 'csrf=blablabla',\n      referer: '',\n    },\n  },\n  response: {\n    locals: {\n      user: {\n        isAuthenticated: false,\n      },\n    },\n  },\n});\n```\n\n## Why not use `apollo-server-testing`?\n\nYou can't really write _real_ integration tests with `apollo-server-testing`, because it doesn't support servers which rely on the `context` option being a function that uses the `req` object ([see this issue for more information](https://github.com/apollographql/apollo-server/issues/2277)).\n\n[Real apollo-servers support this behavior](https://www.apollographql.com/docs/apollo-server/essentials/data/#context-argument), but the test client created with `apollo-server-testing` does not. For example:\n\n```js\nimport { createTestClient } from 'apollo-server-testing';\n\nit('will not work', () =\u003e {\n const { query } = createTestClient(\n   new ApolloServer({\n     schema,\n     context: ({ req }) =\u003e {\n       return doSomethingWithReq(req); // this won't work because `req` is `undefined`.\n     }\n   })\n );\n\n // Any middleware or resolver code that depends on `context` will not work when this runs, because\n // the `context` function does *not* get passed `req` as expected.\n const result = await query(\n   `{ currentUser { id } }`\n )\n});\n```\n\n[The official integration example code from Apollo](https://github.com/apollographql/fullstack-tutorial/blob/6988f6948668ccc2dea3f7a216dd44bdf25a0b9f/final/server/src/__tests__/integration.js#L68-L74) solves this by instantiating an ApolloServer inside the test and mocking the `context` value by hand. But I don't consider this a real integration test, since you're not using the same instantiation code that your production code uses.\n\n## Support\n\nThis package should work for consumers using `apollo-server-express`. We don't plan on supporting any other node server integrations at this time.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzapier%2Fapollo-server-integration-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzapier%2Fapollo-server-integration-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzapier%2Fapollo-server-integration-testing/lists"}