在端到端测试期间如何应用全局管道

时间:2019-12-16 11:36:05

标签: nestjs

使用Test.createTestingModule时如何应用全局管道?

通常,将应用程序安装在main.ts中时会添加全局管道。

beforeEach(async done => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule]
    }).compile()

    app = moduleFixture.createNestApplication()
    await app.init()
    done()
  })

1 个答案:

答案 0 :(得分:0)

您可以在初始化测试模块之前添加它们:

  beforeEach(async done => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule]
    }).compile()

    app = moduleFixture.createNestApplication()

    // Add global pipe here
    app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true, forbidNonWhitelisted: true }))

    await app.init()
    done()
  })