blog-be/test/app.e2e-spec.ts
2021-04-24 14:47:45 +08:00

50 lines
1.3 KiB
TypeScript

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>(
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();
});
});