【问题标题】:Problem with e2e testing with NestJS TestingModule, GraphQL code first and TypeOrm使用 NestJS TestingModule、GraphQL 代码优先和 TypeOrm 进行 e2e 测试的问题
【发布时间】:2020-05-12 03:28:12
【问题描述】:

几天以来,我一直在使用 GraphQL 代码优先方法和 TypeOrm 我的 NestJS 应用程序进行 e2e 测试强>.

我正在尝试通过使用 autoSchemaFile 注入 nestjs GraphQLModule 来创建 TestingModule,但我总是收到错误“ Schema 必须包含唯一命名的类型,但包含多个名为 ...".

的类型

这里用最少的代码复制了我的错误:

character.entity.ts:

import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { ObjectType, Field, ID } from 'type-graphql';

@Entity()
@ObjectType()
export class Character {
    @PrimaryGeneratedColumn()
    @Field(() => ID)
    id: string;

    @Column({ unique: true })
    @Field()
    name: string;
}

character.resolver.ts:

import { Query, Resolver } from '@nestjs/graphql';
import { Character } from './models/character.entity';
import { CharacterService } from './character.service';

@Resolver(() => Character)
export class CharacterResolver {
    constructor(private readonly characterService: CharacterService) {}

    @Query(() => [Character], { name: 'characters' })
    async getCharacters(): Promise<Character[]> {
        return this.characterService.findAll();
    }
}

character.module.ts:

import { Module } from '@nestjs/common';
import { CharacterResolver } from './character.resolver';
import { CharacterService } from './character.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Character } from './models/character.entity';

@Module({
    imports: [TypeOrmModule.forFeature([Character])],
    providers: [CharacterResolver, CharacterService],
})
export class CharacterModule {}

app.module.ts:

import { Module } from '@nestjs/common';
import { CharacterModule } from './character/character.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Connection } from 'typeorm';
import { GraphQLModule } from '@nestjs/graphql';

@Module({
    imports: [TypeOrmModule.forRoot(), GraphQLModule.forRoot({ autoSchemaFile: 'schema.gql' }), CharacterModule],
    controllers: [],
    providers: [],
})
export class AppModule {
    constructor(private readonly connection: Connection) {}
}

最后:character.e2e-spec.ts:

import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CharacterModule } from '../src/character/character.module';
import { GraphQLModule } from '@nestjs/graphql';

describe('CharacterResolver (e2e)', () => {
    let app: INestApplication;

    beforeAll(async () => {
        const module: TestingModule = await Test.createTestingModule({
            imports: [
                TypeOrmModule.forRoot(),
                GraphQLModule.forRoot({ playground: false, autoSchemaFile: 'schema.gql' }),
                CharacterModule,
            ],
        }).compile();

        app = module.createNestApplication();
        await app.init();
    });

    it('should create testing module', () => {
        expect(1).toBe(1);
    });

    afterAll(async () => {
        await app.close();
    });
});

在运行npm run test:e2e之后:

Schema must contain uniquely named types but contains multiple types named "Character".

      at typeMapReducer (../node_modules/graphql/type/schema.js:262:13)
          at Array.reduce (<anonymous>)
      at new GraphQLSchema (../node_modules/graphql/type/schema.js:145:28)
      at Function.generateFromMetadataSync (../node_modules/type-graphql/dist/schema/schema-generator.js:31:24)
      at Function.<anonymous> (../node_modules/type-graphql/dist/schema/schema-generator.js:16:33)
      at ../node_modules/tslib/tslib.js:110:75
      at Object.__awaiter (../node_modules/tslib/tslib.js:106:16)
      at Function.generateFromMetadata (../node_modules/type-graphql/dist/schema/schema-generator.js:15:24)

我没有找到任何其他方法来在官方文档上或在谷歌搜索时使用 graphql 代码优先方法创建测试模块......我错过了什么吗?

【问题讨论】:

  • 今天刚设置,发现同样的问题。不知道是什么导致它。我可以确认错误发生在点:app.init()

标签: graphql e2e-testing nestjs


【解决方案1】:

您的ormconfig.json 需要如下所示:

  "entities": [
    "src/**/*.entity.js"
  ],
  "migrations": [
    "src/migration/*.js"
  ],
  "cli": {
    "migrationsDir": "src/migration"
  }

即您需要指定位于src 中的实体,而不是dist 文件夹。如果不是,TypeGraphQL 将为每个解析器生成两次架构。要使生成和运行迁移命令正常工作,您必须为您的开发环境设置一个不同的ormconfig.json

【讨论】:

    猜你喜欢
    • 2022-07-29
    • 2021-03-02
    • 2020-10-28
    • 2021-10-30
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2020-07-14
    相关资源
    最近更新 更多