【问题标题】:TypeORM fails to find a connection when testing an Express app with SupertestTypeORM 在使用 Supertest 测试 Express 应用程序时无法找到连接
【发布时间】: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 方法,例如findquery,它实际上返回了存储在数据库中的数据,这让我相信我的连接确实已经建立并且正在工作。

另外,我的应用程序在使用node 运行时工作正常,它只是在测试中输出此连接错误。值得一提的是,我正在使用tsyringe 将存储库实现注入我的服务,但我认为这不会导致问题。有人知道这里可能会发生什么吗?

【问题讨论】:

    标签: typescript jestjs typeorm supertest


    【解决方案1】:

    这里发生的事情是我的'ormconfig.js' 文件上有两个连接:

    module.exports = [
      {
        name: 'default',
        type: 'postgres',
        // ...
      },
      {
        name: 'test',
        type: 'postgres',
        // ...
      },
    ];
    

    而且,如果没有另行通知,typeorm 总是会尝试使用'default' 连接来做事。例如,使用getRepository(Teacher) 加载实体存储库相当于getRepository(Teacher, 'default')。所以typeorm 实际上是在创建连接,但它在我运行集成测试时尝试使用'default' 而不是'test'

    当您使用多个连接时,您可以采取一些措施来解决此问题:

    1. 每次都硬编码你想使用的连接(在大多数情况下不会很好);

    2. 设置环境变量并使用它们来选择您想要的连接(这是最灵活的选项);

    3. 在创建连接时,加载所需的连接配置并将其“重命名”为 'default' 或您可能使用的任何其他名称。

    就我而言,我选择了第三个选项,因此我在测试中添加了以下内容:

    beforeAll(async () => {
        const connOpts = await getConnectionOptions('test');
        conn = await createConnection({ ...connOpts, name: 'default' });
        // ...
    });
    

    如果您要使用两个以上的连接,第二个选项可能会更好,但第三个也可以。完全由您决定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 2023-03-17
      • 2020-02-15
      • 1970-01-01
      • 2019-09-04
      相关资源
      最近更新 更多