Apollo Client Test Utilities (Deprecated)

raw JSON →
0.3.2 verified Sun Apr 19 auth: no javascript abandoned

apollo-test-utils is a rudimentary JavaScript package specifically designed to facilitate testing with Apollo Client version 1.x. Released with a low version number (0.3.2), it primarily offered functions like `mockNetworkInterfaceWithSchema` to mock the network interface, a core architectural concept prevalent in Apollo Client's early iterations. This package is no longer maintained, with its last commit dating back to 2017, and is completely incompatible with Apollo Client versions 2.x and above. The original `apollo-client` v1, for which this package was intended, has been superseded by `@apollo/client`. Modern testing approaches for Apollo Client, which has since evolved through several major releases, leverage `@apollo/client/testing` for React components (e.g., `MockedProvider`) or directly mock `ApolloLink` instances for headless client testing. Developers should consider `apollo-test-utils` abandoned and migrate to current official Apollo testing utilities for any active projects.

error TypeError: client.query is not a function
cause Attempting to use `apollo-test-utils` with `ApolloClient` v2+ where the `networkInterface` concept and the associated `query` method signature have changed significantly or are missing.
fix
This package is incompatible with Apollo Client v2+. Update your testing strategy to use @apollo/client/testing or ApolloLink based mocks for modern Apollo Client versions.
error Error: Cannot find module 'apollo-test-utils' or 'apollo-client'
cause The package is either not installed, or you are attempting to use it in a project that has fundamentally diverged from its original dependencies (e.g., trying to use it with `@apollo/client` instead of `apollo-client` v1).
fix
Ensure apollo-test-utils and apollo-client@^1.0.0 are correctly installed. More importantly, consider if you are trying to use an abandoned package with a modern setup. If so, switch to @apollo/client/testing for modern Apollo Client testing.
breaking This package is only compatible with `apollo-client` v1.x. It is not compatible with `apollo-client` v2.x, v3.x, or v4.x due to fundamental architectural changes, including the deprecation of the `networkInterface` concept. The package is abandoned and receives no further updates.
fix For testing Apollo Client v2+, v3+, or v4+ applications, use the official `@apollo/client/testing` package (e.g., `MockedProvider` for React components) or mock `ApolloLink` instances.
deprecated The package `apollo-test-utils` has been abandoned since 2017 and is no longer maintained. Using it in new projects or with modern Apollo Client versions will lead to incompatibility issues.
fix Migrate to current official Apollo Client testing solutions. For React, `@apollo/client/testing` provides `MockedProvider`. For client-agnostic GraphQL testing, consider `@apollo/graphql-testing-library`.
gotcha This package was developed before widespread adoption of ECMAScript Modules (ESM). Using it in modern JavaScript environments or with module bundlers that heavily rely on ESM might lead to unexpected CommonJS/ESM interoperability issues.
fix If strictly needing to run this legacy code, ensure your build setup supports transparent CommonJS interoperability for older packages, but a full migration to modern testing libraries is strongly recommended.
npm install apollo-test-utils
yarn add apollo-test-utils
pnpm add apollo-test-utils

Demonstrates mocking an Apollo Client v1 network interface with a GraphQL schema for basic query testing.

import ApolloClient from 'apollo-client';
import gql from 'graphql-tag';
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
import { mockNetworkInterfaceWithSchema } from 'apollo-test-utils';

const typeDefs = `
    type User {
      id: Int
      name: String
    }

    type Query {
      user: User
    }
  `;

const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({ schema });

const mockNetworkInterface = mockNetworkInterfaceWithSchema({ schema });

const client = new ApolloClient({
  networkInterface: mockNetworkInterface,
});

client.query({
  query: gql`{ user { name } }`,
}).then(result => {
  console.log('Query Result:', result.data.user.name);
});