使用 NestJS TestingModule、GraphQL 代码优先和 TypeOrm 进行端到端测试的问题
Problem with e2e testing with NestJS TestingModule, GraphQL code first and TypeOrm
几天以来,我一直在努力进行 e2e 测试 我的 NestJS 应用程序使用 GraphQL 代码优先方法和 TypeOrm.
我试图通过注入 nestjs GraphQLModule 和 autoSchemaFile 来创建 TestingModule我总是收到错误消息“架构必须包含唯一命名的类型,但包含多个名为 ... 的类型”。
这里用最少的代码重现了我的错误:
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 代码优先方法创建测试模块...我是否遗漏了什么?
您的 ormconfig.json
需要如下所示:
"entities": [
"src/**/*.entity.js"
],
"migrations": [
"src/migration/*.js"
],
"cli": {
"migrationsDir": "src/migration"
}
即您需要指定 src
文件夹中的实体,而不是 dist
文件夹中的实体。如果不是,TypeGraphQL 将为每个解析器生成模式两次。要使生成和 运行 迁移命令起作用,您必须为您的开发环境设置不同的 ormconfig.json
。
几天以来,我一直在努力进行 e2e 测试 我的 NestJS 应用程序使用 GraphQL 代码优先方法和 TypeOrm.
我试图通过注入 nestjs GraphQLModule 和 autoSchemaFile 来创建 TestingModule我总是收到错误消息“架构必须包含唯一命名的类型,但包含多个名为 ... 的类型”。
这里用最少的代码重现了我的错误:
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 代码优先方法创建测试模块...我是否遗漏了什么?
您的 ormconfig.json
需要如下所示:
"entities": [
"src/**/*.entity.js"
],
"migrations": [
"src/migration/*.js"
],
"cli": {
"migrationsDir": "src/migration"
}
即您需要指定 src
文件夹中的实体,而不是 dist
文件夹中的实体。如果不是,TypeGraphQL 将为每个解析器生成模式两次。要使生成和 运行 迁移命令起作用,您必须为您的开发环境设置不同的 ormconfig.json
。