# apollo-server-integration-testing

> Test helper for writing apollo-server integration tests

Latest version **3.0.0** (published 2021-04-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install apollo-server-integration-testing
pnpm add apollo-server-integration-testing
yarn add apollo-server-integration-testing
bun add apollo-server-integration-testing
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2021-04-13 |
| First published | 2019-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 25.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 130 |
| Author | Vitor Balocco |
| Maintainers | vitorbal |
| Keywords | GraphQL, Apollo, Server, Javascript, Testing, Integration Testing |

## Links

- npm: https://www.npmjs.com/package/apollo-server-integration-testing
- Repository: https://github.com/zapier/apollo-server-integration-testing
- Homepage: https://github.com/zapier/apollo-server-integration-testing#readme
- Issues: https://github.com/zapier/apollo-server-integration-testing/issues
- npm.io page: https://npm.io/package/apollo-server-integration-testing

## Dependencies (4)

- [express](https://npm.io/package/express.md) ^4.17.1
- [node-mocks-http](https://npm.io/package/node-mocks-http.md) ^1.8.0
- [apollo-server-core](https://npm.io/package/apollo-server-core.md) ^2.9.13
- [apollo-server-express](https://npm.io/package/apollo-server-express.md) ^2.9.13

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2021-04-13
- 2.3.1 — 2021-03-14
- 2.3.0 — 2019-12-12
- 2.2.0 — 2019-10-04
- 2.1.0 — 2019-09-12
- 2.0.0 — 2019-08-09
- 1.0.1 — 2019-08-09
- 1.0.0 — 2019-08-09

## README

# apollo-server-integration-testing

This package exports an utility function for writing apollo-server integration tests:

```
import { createTestClient } from 'apollo-server-integration-testing';
```

## Usage

This 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.

Example usage:

```js
import { createTestClient } from 'apollo-server-integration-testing';
import { createApolloServer } from './myServerCreationCode';

const apolloServer = await createApolloServer();
const { query, mutate } = createTestClient({
  apolloServer,
});

const result = await query(`{ currentUser { id } }`);

expect(result).toEqual({
  data: {
    currentUser: {
      id: '1',
    },
  },
});

const UPDATE_USER = `
  mutation UpdateUser($id: ID!, $email: String!) {
    updateUser(id: $id, email: $email) {
      user {
        email
      }
    }
  }
`;

const mutationResult = await mutate(UPDATE_USER, {
  variables: { id: 1, email: 'nancy@foo.co' },
});

expect(mutationResult).toEqual({
  data: {
    updateUser: {
      email: 'nancy@foo.co',
    },
  },
});
```

This 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.

### Mocking the `Request` or `Response` object

`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.
You can also extend the mocked Request or Response object with additional keys by passing an `extendMockRequest` or `extendMockResponse` field to `createTestClient`:

```js
const { query } = createTestClient({
  apolloServer,
  extendMockRequest: {
    headers: {
      cookie: 'csrf=blablabla',
      referer: '',
    },
  },
  extendMockResponse: {
    locals: {
      user: {
        isAuthenticated: false,
      },
    },
  },
});
```

This 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.

As 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.

### setOptions

You 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`.

```js
const { query, setOptions } = createTestClient({
  apolloServer,
});

setOptions({
  // If "request" or "response" is not specified, it's not modified
  request: {
    headers: {
      cookie: 'csrf=blablabla',
      referer: '',
    },
  },
  response: {
    locals: {
      user: {
        isAuthenticated: false,
      },
    },
  },
});
```

## Why not use `apollo-server-testing`?

You 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)).

[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:

```js
import { createTestClient } from 'apollo-server-testing';

it('will not work', () => {
 const { query } = createTestClient(
   new ApolloServer({
     schema,
     context: ({ req }) => {
       return doSomethingWithReq(req); // this won't work because `req` is `undefined`.
     }
   })
 );

 // Any middleware or resolver code that depends on `context` will not work when this runs, because
 // the `context` function does *not* get passed `req` as expected.
 const result = await query(
   `{ currentUser { id } }`
 )
});
```

[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.

## Support

This package should work for consumers using `apollo-server-express`. We don't plan on supporting any other node server integrations at this time.

---
_Source: https://npm.io/package/apollo-server-integration-testing · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
