Nestjs 使用TypeORM和ormconfig.js+;同步化

Nestjs 使用TypeORM和ormconfig.js+;同步化,nestjs,e2e-testing,typeorm,Nestjs,E2e Testing,Typeorm,我正在寻找帮助,以便在基本的hello world NestJS项目中设置e2e测试。我使用应用程序根文件夹中的ormconfig.js文件进行数据库配置,因为我使用事务,并且必须能够从命令行运行typeorm cli。这是或mconfig.js: /* eslint-disable @typescript-eslint/no-var-requires */ const config = require('config') const path = require('path') /* esli

我正在寻找帮助,以便在基本的hello world NestJS项目中设置e2e测试。我使用应用程序根文件夹中的
ormconfig.js
文件进行数据库配置,因为我使用事务,并且必须能够从命令行运行typeorm cli。这是
或mconfig.js

/* eslint-disable @typescript-eslint/no-var-requires */
const config = require('config')
const path = require('path')
/* eslint-enable */

const dbConfig = config.get('db')

module.exports = {
  type: dbConfig.type,
  host: dbConfig.host,
  port: dbConfig.port,
  username: dbConfig.username,
  password: dbConfig.password,
  database: dbConfig.database,
  syncronize: dbConfig.syncronize,
  dropSchema: dbConfig.dropSchema,
  entities: [path.join(__dirname, 'dist', 'entities', '*.{ts,js}')],
  migrations: [path.join(__dirname, 'dist', 'migrations', '*.{ts,js}')]
}
然后,我有一个非常基本的
app.module.ts

import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
import { AuthModule } from '../auth/auth.module'

@Module({
  imports: [TypeOrmModule.forRoot(), AuthModule]
})
export class AppModule {}
最后是e2e测试文件:

import { INestApplication } from '@nestjs/common'
import { Test, TestingModule } from '@nestjs/testing'
import * as request from 'supertest'
import { AppModule } from '../src/app/app.module'

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

  beforeAll(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule]
    }).compile()
    app = moduleFixture.createNestApplication()
    await app.init()
  })

  it('/auth/signup (POST) should sign up the user', () => {
    return request(app.getHttpServer())
      .post('/auth/signup')
      .send({
        email: 'test@test.com',
        password: '123123qQ'
      })
      .expect(201)
  })

  afterAll(async () => {
    await app.close()
  })
})
我可以运行迁移和应用程序本身,但当尝试运行e2e测试时,我得到
EntityMetadataNotFound:未找到“用户”的元数据。

我猜,TypeORM在测试期间以不同的方式处理配置。我尝试了不同的方法来设置连接,最有效的方法似乎是手动设置实体:

app.module.ts:
...
import * as typeOrmConfig from '../../ormconfig'
import { User } from '../entities/user'

@Module({
  imports: [
    TypeOrmModule.forRoot({
      ...typeOrmConfig,
      entities: [User]
    }),
    AuthModule
  ]
})
export class AppModule {}
指定所有实体并不方便(我正在寻找避免这种情况的方法),但至少我要进入下一步,现在错误是ER_NO_这样的表。我有单独的测试数据库,它是空的。但是在测试的类型表中有
syncronize:true
。syncronize是否存在异步问题?是否可能以某种方式获得对连接对象的访问并等待connection.syncronize(true)在beforeAll中