{"id":12813,"library":"apollo-server-testing","title":"Apollo Server Testing Utilities (Legacy)","description":"This `apollo-server-testing` package provides utilities specifically designed for testing applications built with older versions of Apollo Server (v2 and v3). Its primary export, `createTestClient`, wraps the `ApolloServer.executeOperation` method to allow direct GraphQL operation execution against an `ApolloServer` instance without spinning up a full HTTP server. The package `apollo-server-testing` is currently at version `2.25.3` and was last published over four years ago. It is considered abandoned for new development, as the current major versions of Apollo Server (v4 and v5, under the `@apollo/server` package) integrate testing utilities directly via `server.executeOperation` or provide `@apollo/server/testing` where applicable. This package is solely relevant for maintaining legacy projects still on `apollo-server` v2 or v3, which are both officially end-of-life.","status":"abandoned","version":"2.25.3","language":"javascript","source_language":"en","source_url":"https://github.com/apollographql/apollo-server","tags":["javascript","GraphQL","Apollo","Server","Javascript","typescript"],"install":[{"cmd":"npm install apollo-server-testing","lang":"bash","label":"npm"},{"cmd":"yarn add apollo-server-testing","lang":"bash","label":"yarn"},{"cmd":"pnpm add apollo-server-testing","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for GraphQL schema definition and execution, a peer dependency for Apollo Server.","package":"graphql","optional":false},{"reason":"This package is a testing utility for the Apollo Server core, specifically versions 2.x and 3.x.","package":"apollo-server","optional":false}],"imports":[{"note":"This specific import is for `apollo-server-testing` (v2/v3). For `@apollo/server` (v4+), testing is typically done directly with `server.executeOperation` or `@apollo/server/testing` if available for specific integrations. The `apollo-server-testing` package is deprecated and will not be published with Apollo Server 3 onwards.","wrong":"import { createTestClient } from '@apollo/server/testing';","symbol":"createTestClient","correct":"import { createTestClient } from 'apollo-server-testing';"},{"note":"This package integrates with `apollo-server` (v2/v3). The modern equivalent for v4+ is `@apollo/server` which has a different import path and API. Make sure you are using the correct `ApolloServer` instance that matches your project's main Apollo Server version.","wrong":"import { ApolloServer } from '@apollo/server';","symbol":"ApolloServer","correct":"import { ApolloServer, gql } from 'apollo-server';"}],"quickstart":{"code":"import { ApolloServer, gql } from 'apollo-server';\nimport { createTestClient } from 'apollo-server-testing';\n\n// 1. Define your GraphQL schema\nconst typeDefs = gql`\n  type Query {\n    hello(name: String): String!\n    getUser(id: ID!): User\n  }\n\n  type User {\n    id: ID!\n    name: String!\n    email: String!\n  }\n\n  type Mutation {\n    addUser(name: String!, email: String!): User!\n  }\n`;\n\n// 2. Define your resolvers\nconst users = [\n  { id: '1', name: 'Alice', email: 'alice@example.com' },\n  { id: '2', name: 'Bob', email: 'bob@example.com' }\n];\n\nconst resolvers = {\n  Query: {\n    hello: (_, { name }) => `Hello ${name || 'World'}!`,\n    getUser: (_, { id }) => users.find(user => user.id === id)\n  },\n  Mutation: {\n    addUser: (_, { name, email }) => {\n      const newUser = { id: String(users.length + 1), name, email };\n      users.push(newUser);\n      return newUser;\n    }\n  }\n};\n\n// 3. Create an Apollo Server instance\nconst server = new ApolloServer({\n  typeDefs,\n  resolvers,\n  // Optional: context function\n  context: () => ({ /* Add any context relevant for your resolvers */ })\n});\n\n// 4. Create a test client\nconst { query, mutate } = createTestClient(server);\n\n// Example Test (using Jest syntax for illustration)\ndescribe('Apollo Server Integration Tests', () => {\n  it('should fetch a greeting', async () => {\n    const GET_HELLO = gql`\n      query GetHello($name: String) {\n        hello(name: $name)\n      }\n    `;\n    const response = await query({ query: GET_HELLO, variables: { name: 'Test' } });\n    expect(response.data.hello).toBe('Hello Test!');\n  });\n\n  it('should add a new user', async () => {\n    const ADD_USER = gql`\n      mutation AddUser($name: String!, $email: String!) {\n        addUser(name: $name, email: $email) {\n          id\n          name\n          email\n        }\n      }\n    `;\n    const response = await mutate({ query: ADD_USER, variables: { name: 'Charlie', email: 'charlie@example.com' } });\n    expect(response.data.addUser).toEqual({\n      id: expect.any(String),\n      name: 'Charlie',\n      email: 'charlie@example.com'\n    });\n    expect(users).toHaveLength(3);\n  });\n\n  it('should fetch an existing user', async () => {\n    const GET_USER = gql`\n      query GetUser($id: ID!) {\n        getUser(id: $id) {\n          id\n          name\n          email\n        }\n      }\n    `;\n    const response = await query({ query: GET_USER, variables: { id: '1' } });\n    expect(response.data.getUser).toEqual(users[0]);\n  });\n});","lang":"typescript","description":"This quickstart demonstrates how to use `apollo-server-testing` with `apollo-server` (v2/v3) to create an in-memory test client and execute GraphQL queries and mutations against a defined schema and resolvers, simulating requests without a live HTTP server. This is suitable for integration tests of the GraphQL layer."},"warnings":[{"fix":"For new projects or migrations, switch to `@apollo/server` (v4+) and utilize `server.executeOperation` directly for testing, or use specific integration testing packages like `apollo-server-integration-testing` if full HTTP layer simulation is needed.","message":"The `apollo-server-testing` package is designed for Apollo Server v2 and v3. Both v2 (end-of-life Oct 2023) and v3 (end-of-life Oct 2024) are no longer officially supported by Apollo. New projects should use `@apollo/server` (v4+) and its integrated testing approaches.","severity":"breaking","affected_versions":">=3.0.0 (of `apollo-server`)"},{"fix":"If your context function depends on `req` or `res`, you'll need to mock these objects within your test context or adjust your context logic for testing. Alternatively, consider using `server.executeOperation` directly and explicitly passing a test-specific context object, or using an integration testing package that properly mocks HTTP requests and responses.","message":"The `createTestClient` function from `apollo-server-testing` does not fully support the `context` function argument that Apollo Server's `executeOperation` method (and the server itself) provides. Specifically, it might not pass `req` or `res` objects to your context function as expected, leading to `undefined` values if your context relies on them.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Migrate your tests to directly use the `ApolloServer` instance's `executeOperation` method. This method provides the same core functionality and is the officially supported way to test GraphQL operations in isolation for Apollo Server v2+. For Apollo Server v4+, the new core `@apollo/server` package has `executeOperation` built-in.","message":"The `apollo-server-testing` package is explicitly deprecated and will not be published with Apollo Server 3 or later. Its functionality is a thin wrapper around `ApolloServer.executeOperation`, which is the recommended direct approach for testing Apollo Server logic without the HTTP layer.","severity":"deprecated","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure your `ApolloServer` instance (from `apollo-server` v2/v3) is correctly constructed before passing it to `createTestClient`. If using `@apollo/server` v4+, you should directly use `server.executeOperation()` instead of `createTestClient()`.","cause":"This typically occurs if `createTestClient` is called with an `ApolloServer` instance that hasn't been properly initialized or started, or if `apollo-server-testing` is being used with `@apollo/server` (v4+), which doesn't directly expose `createTestClient`.","error":"TypeError: Cannot destructure property 'query' of 'undefined' as it is undefined."},{"fix":"Install the required `graphql` version: `npm install graphql@^14.0.0 || ^15.0.0` or `yarn add graphql@^14.0.0 || ^15.0.0`. Refer to your `apollo-server` version's documentation for exact compatible `graphql` versions.","cause":"Missing `graphql` peer dependency or an incompatible version is installed. `apollo-server-testing` depends on `apollo-server` which has `graphql` as a peer dependency.","error":"Error: Apollo Server requires 'graphql' to be installed. Please install 'graphql@^14.0.0 || ^15.0.0' or newer."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null}