import ... from 'nexus/testing'

Guideissues (features | bugs)

This section is about the testing module of the nexus package. This module houses the testing component of Nexus which is is designed to cover your fundamental testing needs at the system level. Namely it gives you an API that allows you to control your app start and send GraphQL operations via a pre-configured client. Nothing is mocked. If you are new to this component then you may be interested in our testing guide.

createTestContext

Setup a test context providing utilities to query against your GraphQL API.

This context can be augmented by plugins.

Signature

1function createTestContext(opts?: CreateTestContextOptions): Promise<TestContext>

Example with Jest

1import { TestContext, createTestContext } from 'nexus/testing'
2
3let ctx: TestContext
4
5beforeAll(async () => {
6 Object.assign(ctx, await createTestContext())
7 await ctx.app.start()
8})
9
10afterAll(async () => {
11 await ctx.app.stop()
12})
13
14test('hello', async () => {
15 const result = await ctx.query(`{ hello }`)
16
17 expect(result).toMatchInlineSnapshot()
18})

I CreateTestContextOptions

1export interface CreateTestContextOptions {
2 /**
3 * A path to the entrypoint of your app. Only necessary if the entrypoint falls outside of Nexus convention.
4 * You should typically use this if you're using `nexus dev --entrypoint` or `nexus build --entrypoint`.
5 */
6 entrypointPath?: string
7 /**
8 * Nexus usually determines the project root by the first `package.json` found while traversing up the file system.
9 * In some cases, e.g. usage in a monorepo, this might not always be correct.
10 * For those cases, you can specify the `projectRoot` manually.
11 *
12 * Example: `await createTestContext({ projectRoot: path.join(__dirname, '../..') })`
13 */
14 projectRoot?: string
15}

I TestContext

1export interface TestContext {
2 client: {
3 send: <T>(query: string, variables: Record<string, any>): Promise<T>
4 headers: {
5 set(headers: Record<string, string>): void
6 set(name: string, value: string): void
7 set(header: [string, string]): void
8 add(headers: Record<string, string>): void
9 add(name: string, value: string): void
10 add(header: [string, string]): void
11 del(name: string): void
12 get(name: string): null | string
13 has(name: string): boolean
14 entries(): [string,string][]
15 }
16 }
17 app: {
18 server: {
19 start: () => Promise<void>
20 stop: () => Promise<void>
21 }
22 }
23}
Edit this page on Github