如何注册cookie中间件进行端到端测试??- @nestjs/测试和认证适配器



我使用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()
})

最新更新