NestJs 和 Jest:等待请求抛出 404
NestJs and Jest: await request throws 404
我目前正在尝试获取 'supertest' 请求的响应对象。
如果我在没有等待的情况下调用 get,我会得到一个 httpCode 200,但没有正文:
import { Test, TestingModule } from '@nestjs/testing';
import { AuthModule } from './auth.module';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
describe('AuthService', () => {
let app: INestApplication;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthModule]
}).compile();
app = module.createNestApplication();
await app.init();
});
it('should be defined', async () => {
const res = request(app.getHttpServer())
.get('/')
.expect(200);
});
afterAll(async () => {
app.close();
});
});
Jest 给我以下输出。但是我不能参考 res.body
AuthService
√ should be defined (5ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 15.961s, estimated 16s
现在,如果我将 get 调用更改为异步调用:
it('should be defined', async () => {
const res = await request(app.getHttpServer())
.get('/')
.expect(200);
});
我得到一个失败的测试结果:
AuthService
× should be defined (35ms)
● AuthService › should be defined
expected 200 "OK", got 404 "Not Found"
at Test.Object.<anonymous>.Test._assertStatus (node_modules/supertest/lib/test.js:268:12)
at Test.Object.<anonymous>.Test._assertFunction (node_modules/supertest/lib/test.js:283:11)
at Test.Object.<anonymous>.Test.assert (node_modules/supertest/lib/test.js:173:18)
at Server.localAssert (node_modules/supertest/lib/test.js:131:12)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
没有异步调用,我无法引用正文。但是我每次都在同一个 get 函数上得到 404。刚刚用于等待异步调用。
没有异步的测试通过只是因为测试在您的断言 expect(200)
运行之前完成。在这两种情况下,调用 /
都会 return 出现 404 错误。
主要问题是您将模块声明为提供者,而不是导入它:
await Test.createTestingModule({
// should be imports instead of providers
providers: [AuthModule]
})
为什么要将 AuthModule
与应用程序的其余部分分开设置?我建议您分开测试您的单元测试(仅包括被测试的提供者,模拟其他所有内容)并在 e2e 测试中测试您的整个应用程序(如有必要,仅模拟应用程序的不同部分,例如 API调用第三方服务);有关详细信息,请参阅 。
我建议改为导入 AppModule
:
const module: TestingModule = await Test.createTestingModule({
imports: [AppModule]
}).compile();
我目前正在尝试获取 'supertest' 请求的响应对象。
如果我在没有等待的情况下调用 get,我会得到一个 httpCode 200,但没有正文:
import { Test, TestingModule } from '@nestjs/testing';
import { AuthModule } from './auth.module';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
describe('AuthService', () => {
let app: INestApplication;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthModule]
}).compile();
app = module.createNestApplication();
await app.init();
});
it('should be defined', async () => {
const res = request(app.getHttpServer())
.get('/')
.expect(200);
});
afterAll(async () => {
app.close();
});
});
Jest 给我以下输出。但是我不能参考 res.body
AuthService
√ should be defined (5ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 15.961s, estimated 16s
现在,如果我将 get 调用更改为异步调用:
it('should be defined', async () => {
const res = await request(app.getHttpServer())
.get('/')
.expect(200);
});
我得到一个失败的测试结果:
AuthService
× should be defined (35ms)
● AuthService › should be defined
expected 200 "OK", got 404 "Not Found"
at Test.Object.<anonymous>.Test._assertStatus (node_modules/supertest/lib/test.js:268:12)
at Test.Object.<anonymous>.Test._assertFunction (node_modules/supertest/lib/test.js:283:11)
at Test.Object.<anonymous>.Test.assert (node_modules/supertest/lib/test.js:173:18)
at Server.localAssert (node_modules/supertest/lib/test.js:131:12)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
没有异步调用,我无法引用正文。但是我每次都在同一个 get 函数上得到 404。刚刚用于等待异步调用。
没有异步的测试通过只是因为测试在您的断言 expect(200)
运行之前完成。在这两种情况下,调用 /
都会 return 出现 404 错误。
主要问题是您将模块声明为提供者,而不是导入它:
await Test.createTestingModule({
// should be imports instead of providers
providers: [AuthModule]
})
为什么要将 AuthModule
与应用程序的其余部分分开设置?我建议您分开测试您的单元测试(仅包括被测试的提供者,模拟其他所有内容)并在 e2e 测试中测试您的整个应用程序(如有必要,仅模拟应用程序的不同部分,例如 API调用第三方服务);有关详细信息,请参阅
我建议改为导入 AppModule
:
const module: TestingModule = await Test.createTestingModule({
imports: [AppModule]
}).compile();