blog-be/test/app.e2e-spec.ts

50 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-04-17 13:54:26 +08:00
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { AppModule } from './../src/app.module';
2021-04-24 14:47:45 +08:00
import { GraphQLModule } from '@nestjs/graphql';
import {
ApolloServerTestClient,
createTestClient,
} from 'apollo-server-testing';
import { gql } from 'apollo-server-express';
2021-04-17 13:54:26 +08:00
2021-04-24 14:47:45 +08:00
describe('ArticleResolver (e2e)', () => {
2021-04-17 13:54:26 +08:00
let app: INestApplication;
2021-04-24 14:47:45 +08:00
let apolloClient: ApolloServerTestClient;
2021-04-17 13:54:26 +08:00
2021-04-24 14:47:45 +08:00
beforeAll(async () => {
2021-04-17 13:54:26 +08:00
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
2021-04-24 14:47:45 +08:00
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!',
},
});
2021-04-17 13:54:26 +08:00
});
2021-04-24 14:47:45 +08:00
afterAll(async () => {
await app?.close();
2021-04-17 13:54:26 +08:00
});
});