我使用pactum和jest进行e2e测试
但是当测试设置cookie的端点时,nest抛出一个错误
[Nest] 3364 - 01/03/2023, 5:35:15 PM ERROR [ExceptionsHandler] response.setCookie is not a function
TypeError: response.setCookie is not a function
但是如何为测试模块注册cookie中间件??app.e2e-spec.ts
let app: INestApplication
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile()
app = moduleRef.createNestApplication<NestFastifyApplication>(
new FastifyAdapter(),
)
app.useGlobalPipes(new ValidationPipe())
await app.init()
await app.getHttpAdapter().getInstance().ready()
await app.listen(3333)
})
afterAll(() => {
app.close()
})
就像您在main.ts
中注册它一样。在调用app.listen()
之前,需要调用app.register()
来注册中间件
let app: NestFastifyApplication
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile()
app = moduleRef.createNestApplication<NestFastifyApplication>(
new FastifyAdapter(),
)
app.useGlobalPipes(new ValidationPipe())
app.register(fastifyCookie, cookieOptions) // register the middleware
await app.init()
await app.getHttpAdapter().getInstance().ready()
await app.listen(3333)
})
afterAll(() => {
app.close()
})