import { Test, TestingModule } from '@nestjs/testing'; import { INestApplication } from '@nestjs/common'; import { AppModule } from './../src/app.module'; import { GraphQLModule } from '@nestjs/graphql'; import { ApolloServerTestClient, createTestClient, } from 'apollo-server-testing'; import { gql } from 'apollo-server-express'; describe('ArticleResolver (e2e)', () => { let app: INestApplication; let apolloClient: ApolloServerTestClient; beforeAll(async () => { const moduleFixture: TestingModule = await Test.createTestingModule({ imports: [AppModule], }).compile(); app = moduleFixture.createNestApplication(); await app.init(); const module: GraphQLModule = moduleFixture.get( GraphQLModule, ); // apolloServer is protected, we need to cast module to any to get it apolloClient = createTestClient((module as any).apolloServer); }); it('QUERY hello', async () => { const res = await apolloClient.query({ query: gql` query { hello { message } } `, variables: {}, }); expect(res.data).toEqual({ hello: { message: 'Hello, World!', }, }); }); afterAll(async () => { await app?.close(); }); });