【发布时间】:2021-11-13 06:18:31
【问题描述】:
我正在构建一个 TypeScript Node.js/Express 应用程序,并开始使用 Jest 和 Supertest 实现一些集成测试,但即使在成功设置 TypeORM 连接后,我的测试也失败了,说没有找到连接。
这是我目前在测试文件中的内容:
let conn: Connection;
describe('tests admin routes', () => {
beforeAll(async () => {
conn = await createConnection();
registerDeps(); // registers dependencies on my tsyringe container
});
afterAll(async () => {
await conn.close();
});
it('should be able to authenticate admins', async () => {
const { status } = await supertest(app) // app is an `Express` "instance"
.post('/admins/auth')
.send({
email: 'myemail@company.com',
password: 'mypassword',
});
console.log(status);
});
});
如果我在终端上运行jest,我会得到以下信息:
FAIL src/modules/admin/web/controllers/__tests__/AdminsController.spec.ts
● Test suite failed to run
ConnectionNotFoundError: Connection "default" was not found.
at new ConnectionNotFoundError (src/error/ConnectionNotFoundError.ts:8:9)
at ConnectionManager.Object.<anonymous>.ConnectionManager.get (src/connection/ConnectionManager.ts:40:19)
at Object.getRepository (src/index.ts:284:35)
at new VideosRepositoryTORM (src/modules/course/infrastructure/lib/typeorm/repositories/VideosRepositoryTORM.ts:11:26)
at Object.<anonymous> (src/modules/course/web/controller/CoursesController.ts:12:26)
at Object.<anonymous> (src/modules/course/web/routers/courses.router.ts:5:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 3.89 s
Ran all test suites.
我有点困惑,因为我的控制器调用的所有服务都没有使用 VideosRepositoryTORM,如果我使用 conn 来解析存储库,例如使用 conn.getRepository(Admin)(Admin 是一个 TypeORM 实体)然后调用任何Repository 方法,例如find 或query,它实际上返回了存储在数据库中的数据,这让我相信我的连接确实已经建立并且正在工作。
另外,我的应用程序在使用node 运行时工作正常,它只是在测试中输出此连接错误。值得一提的是,我正在使用tsyringe 将存储库实现注入我的服务,但我认为这不会导致问题。有人知道这里可能会发生什么吗?
【问题讨论】:
标签: typescript jestjs typeorm supertest