嵌套找不到GraphQLModule元素

问题描述 投票:0回答:1

我正在尝试为我的后端应用程序(nestjs,grpahql,mongodb)编写e2e测试。这是我的测试:

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

我遇到此错误:

嵌套找不到GraphQLModule元素(此提供程序在当前上下文中不存在)

有人可以分享一个有效的例子或给我指出什么地方错了吗?

graphql nest nestjs e2e-testing
1个回答
0
投票

似乎GraphQLModule没有导入到TestModule的范围内。如果是这样,上下文将永远无法使用get()提供它。

此外,这可能对您没有帮助,但这是我们在项目中所做的:

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

我没有添加所有导入,但是这里是最相关的:

import ApolloClient, { gql, InMemoryCache } from 'apollo-boost';
import fetch from 'node-fetch';

我想你知道其他人和/或不需要他们

© www.soinside.com 2019 - 2024. All rights reserved.