Apollo Client Test Utilities (Deprecated)
raw JSON →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.
Common errors
error TypeError: client.query is not a function ↓
@apollo/client/testing or ApolloLink based mocks for modern Apollo Client versions. error Error: Cannot find module 'apollo-test-utils' or 'apollo-client' ↓
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. Warnings
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. ↓
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. ↓
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. ↓
Install
npm install apollo-test-utils yarn add apollo-test-utils pnpm add apollo-test-utils Imports
- mockNetworkInterfaceWithSchema wrong
const { mockNetworkInterfaceWithSchema } = require('apollo-test-utils');correctimport { mockNetworkInterfaceWithSchema } from 'apollo-test-utils'; - ApolloClient wrong
import { ApolloClient } from '@apollo/client';correctimport ApolloClient from 'apollo-client'; - gql wrong
import { gql } from '@apollo/client';correctimport gql from 'graphql-tag';
Quickstart
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);
});