Reputation: 15232
I'm trying to write the e2e test for my backend app (nestjs, grpahql, mongodb). This is my test:
import { INestApplication } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { Test, TestingModule } from '@nestjs/testing';
import {
ApolloServerTestClient,
createTestClient,
} from 'apollo-server-testing';
import gql from 'graphql-tag';
import { UserModule } from '../src/user/user.module';
import { MongooseModule } from '@nestjs/mongoose';
import config from '../src/environments/environment';
describe('User', () => {
let app: INestApplication;
let apolloClient: ApolloServerTestClient;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [UserModule, MongooseModule.forRoot(config.mongoURI)],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
const module: GraphQLModule = moduleFixture.get<GraphQLModule>(
GraphQLModule,
);
apolloClient = createTestClient((module as any).apolloServer);
});
it('should get users', async () => {
const { query } = apolloClient;
const result: any = await query({
query: gql`
query {
getUsers {
_id
name
}
}
`,
variables: {},
});
console.log(result);
});
});
I face this error:
Nest could not find GraphQLModule element (this provider does not exist in the current context)
Could someone share a working example or point me what is wrong?
Upvotes: 2
Views: 8045
Reputation: 2720
It looks like GraphQLModule is not imported in the scope of your TestModule. If so, the context will never be able to provide it using get()
.
Also, this might not help you but this is what we do in our projects:
beforeAll(async () => {
const TCP_PORT = 4242;
const testingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
gqlClient = new ApolloClient({
uri: `http://localhost:${TCP_PORT}/graphql`,
fetch: fetch as any,
cache: new InMemoryCache({
addTypename: false,
}),
});
app = testingModule.createNestApplication();
await app.listen(TCP_PORT);
});
I did not add all the imports but here are the most relevant:
import ApolloClient, { gql, InMemoryCache } from 'apollo-boost';
import fetch from 'node-fetch';
I assume you know the other ones and/or don't need them
Upvotes: 4