【问题标题】:How to apply Global Pipes during e2e tests如何在 e2e 测试期间应用 Global Pipes
【发布时间】:2020-04-08 20:53:17
【问题描述】:

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

通常,当应用程序挂载到main.ts 时,会添加全局管道。

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

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

【问题讨论】:

    标签: nestjs


    【解决方案1】:

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

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

    【讨论】:

    • 应该有一种方法可以从 main.ts 中的内容中自动包含此类内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    相关资源
    最近更新 更多