Docs
Launch GraphOS Studio
Apollo Server 3 is officially deprecated, with end-of-life scheduled for 22 October 2024. Learn more about upgrading to a supported Apollo Server version.

Integration testing

Utilities for testing Apollo Server


uses a multi-step request pipeline to validate and execute incoming . This pipeline supports integration with custom plugins at each step, which can affect an operation's execution. Because of this, it's important to perform integration tests with a variety of operations to ensure your request pipeline works as expected.

There are two main options for integration testing with Apollo Server:

  • Using ApolloServer's executeOperation method.
  • Setting up an HTTP client to your server.

Testing using executeOperation

Apollo Server's executeOperation method enables you to run operations through the request pipeline without sending an HTTP request.

The executeOperation method accepts the following :

  • An object that describes the GraphQL to execute.
    • This object must include a query specifying the GraphQL operation to run. You can use executeOperation to execute both queries and , but both use the query field.
  • An optional that is passed in to the ApolloServer instance's context function.

Below is a simplified example of setting up a test using the JavaScript testing library Jest:

index.test.ts
// For clarity in this example we included our typeDefs and resolvers above our test,
// but in a real world situation you'd be importing these in from different files
const typeDefs = gql`
type Query {
hello(name: String): String!
}
`;
const resolvers = {
Query: {
hello: (_, { name }) => `Hello ${name}!`,
},
};
it('returns hello with the provided name', async () => {
const testServer = new ApolloServer({
typeDefs,
resolvers,
});
const result = await testServer.executeOperation({
query: 'query SayHelloWorld($name: String) { hello(name: $name) }',
variables: { name: 'world' },
});
expect(result.errors).toBeUndefined();
expect(result.data?.hello).toBe('Hello world!');
});
index.test.js
// For clarity in this example we included our typeDefs and resolvers above our test,
// but in a real world situation you'd be importing these in from different files
const typeDefs = gql`
type Query {
hello(name: String): String!
}
`;
const resolvers = {
Query: {
hello: (_, { name }) => `Hello ${name}!`,
},
};
it('returns hello with the provided name', async () => {
const testServer = new ApolloServer({
typeDefs,
resolvers,
});
const result = await testServer.executeOperation({
query: 'query SayHelloWorld($name: String) { hello(name: $name) }',
variables: { name: 'world' },
});
expect(result.errors).toBeUndefined();
expect(result.data?.hello).toBe('Hello world!');
});

Note that when testing, any errors in parsing, validating, and executing your GraphQL operation are returned in the errors field of the operation result. As with any GraphQL response, these errors are not thrown.

Unlike with a normal instance of ApolloServer, you don't need to call start before calling executeOperation. The server instance calls start automatically for you if it hasn't been called already, and any startup errors are thrown.

To expand on the example above, here's a full integration test being run against a test instance of ApolloServer. This test imports all of the important pieces to test (typeDefs, resolvers, dataSources) and creates a new instance of ApolloServer.

integration.test.js
it('fetches single launch', async () => {
const userAPI = new UserAPI({ store });
const launchAPI = new LaunchAPI();
// create a test server to test against, using our production typeDefs,
// resolvers, and dataSources.
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({ userAPI, launchAPI }),
context: () => ({ user: { id: 1, email: 'a@a.a' } }),
});
// mock the dataSource's underlying fetch methods
launchAPI.get = jest.fn(() => [mockLaunchResponse]);
userAPI.store = mockStore;
userAPI.store.trips.findAll.mockReturnValueOnce([
{ dataValues: { launchId: 1 } },
]);
// run the query against the server and snapshot the output
const res = await server.executeOperation({ query: GET_LAUNCH, variables: { id: 1 } });
expect(res).toMatchSnapshot();
});

The example above includes a test-specific context function, which provides data directly to the ApolloServer instance instead of calculating it from the request's context.

To use your server's defined context function, you can pass a second argument to executeOperation, which is then passed to your server's context function. Note that to use your server's context function you need to put together an object with the correct middleware-specific context fields for your implementation.

For examples of both integration and end-to-end testing we recommend checking out the tests included in the Apollo fullstack tutorial.

End-to-end testing

Instead of bypassing the HTTP layer, you might want to fully run your server and test it with a real HTTP client. Apollo Server doesn't provide built-in support for this at this time.

You can run operations against your server using a combination of any HTTP or such as supertest or Apollo Client's HTTP Link . There are also community packages available such as apollo-server-integration-testing, which uses mocked Express request and response objects.

Below is an example of writing an end-to-end test using the apollo-server package and supertest:

server.test.ts
/// we import a function that we wrote to create a new instance of Apollo Server
import { createApolloServer } from '../server';
// we will use supertest to test our server
import request from 'supertest';
// this is the query we use for our test
const queryData = {
query: `query sayHello($name: String) {
hello(name: $name)
}`,
variables: { name: 'world' },
};
describe('e2e demo', () => {
let server, url;
// before the tests we will spin up a new Apollo Server
beforeAll(async () => {
// Note we must wrap our object destructuring in parentheses because we already declared these variables
// We pass in the port as 0 to let the server pick its own ephemeral port for testing
({ server, url } = await createApolloServer({ port: 0 }));
});
// after the tests we will stop our server
afterAll(async () => {
await server?.close();
});
it('says hello', async () => {
// send our request to the url of the test server
const response = await request(url).post('/').send(queryData);
expect(response.errors).toBeUndefined();
expect(response.body.data?.hello).toBe('Hello world!');
});
});
server.test.js
/// we import a function that we wrote to create a new instance of Apollo Server
import { createApolloServer } from '../server';
// we will use supertest to test our server
import request from 'supertest';
// this is the query we use for our test
const queryData = {
query: `query sayHello($name: String) {
hello(name: $name)
}`,
variables: { name: 'world' },
};
describe('e2e demo', () => {
let server, url;
// before the tests we will spin up a new Apollo Server
beforeAll(async () => {
// Note we must wrap our object destructuring in parentheses because we already declared these variables
// We pass in the port as 0 to let the server pick its own ephemeral port for testing
({ server, url } = await createApolloServer({ port: 0 }));
});
// after the tests we will stop our server
afterAll(async () => {
await server?.close();
});
it('says hello', async () => {
// send our request to the url of the test server
const response = await request(url).post('/').send(queryData);
expect(response.errors).toBeUndefined();
expect(response.body.data?.hello).toBe('Hello world!');
});
});

You can also view and fork this complete example on CodeSandbox:

Edit integration-testing
Previous
Mocking
Next
Caching
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company