【发布时间】:2021-04-06 23:29:53
【问题描述】:
我正在尝试使用以下代码在 nestjs 中编写我的第一个 e2e 测试:
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [
AppModule,
]
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
await getConnection().synchronize(true);
});
it('/ (GET)', async () => {
return request(app.getHttpServer())
.get('/')
.expect(200);
});
afterAll(async () => {
await app.close();
});
});
尝试调试我看到的超测试请求时,它总是以 404 失败:
{
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 57523,
response: undefined
}
如果我尝试使用.agent('127.0.0.1:3000') 连接到实际应用程序,则测试通过。
it('/ (GET)', async () => {
return request
.agent('127.0.0.1:3000')
.get('/')
.expect(200)
});
【问题讨论】: