如何使用 Jest 在 TypeORM 中存根 EntityManager 和 Connection [英] How to stub EntityManager and Connection in TypeORM with Jest

查看:28
本文介绍了如何使用 Jest 在 TypeORM 中存根 EntityManager 和 Connection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 TypeORM 和用 Jest 编写的单元测试在 Typescript 中的 NestJS 上获得了一个应用程序.我有一个使用这样的事务的函数:

I got an app on NestJS in Typescript using TypeORM and unit-tests written with Jest. I have a function that uses transactions like this:

async createMany(users: User[]) {
  await this.connection.transaction(async manager => {
    await manager.save(users[0]);
    await manager.save(users[1]);
  });
}

这是来自 NestJS 文档的示例.我通过 this.connection.transaction 以大致相同的方式完成,但业务逻辑不同.

That's an example from NestJS docs. I do it roughly in the same way via this.connection.transaction but the business-logic is different.

问题是我想做一个单元测试来测试这个服务功能.所以我需要以某种方式模拟 this.connection 和它的 manager.或者至少是经理.我不确定如何使用 Jest 做到这一点.我无法在没有连接的情况下创建管理器.我无法创建一个没有管理器返回内部的模拟连接.

The thing is I want to make a unit-test to test this service function. So I need to somehow mock both this.connection and its manager. Or at least the manager. I'm not sure how to do it using Jest. I can't create a manager without a connection. I can't create a mock connection with no manager to return inside it.

同时使用 TypeORM 和 Jest 是 NestJS 中的标准.必须有一种方法来编写带有事务的单元测试.但我不知道该怎么做.

Using both TypeORM and Jest is standard in NestJS. There have to be a way to write unit-tests with transactions. But I am not sure how to do it.

请注意,我问的是单元测试模拟 ORM.不是直接使用测试数据库实例的集成测试.

Note that I am asking about unit-test mocking ORM. Not integration tests that would directly use a testing db instance.

推荐答案

您需要使用模拟的 Connection 创建一个 TestingModule.像这样的东西:

You would need to create a TestingModule with a mocked Connection. Something likes this:

import { Test } from "@nestjs/testing";

describe("UsersService", () => {
    let usersService;
    let connection;
    const mockConnection = () => ({
        transaction: jest.fn()
    });

    beforeEach(async () => {
        const module = await Test.createTestingModule({
            providers: [
                UsersService,
                {
                    provide: Connection,
                    useFactory: mockConnection
                }
            ],
        }).compile();

        usersService = await module.get<UsersService>(UsersService); 
        connection = await modle.get<Connection>(Connection);
    });

    describe("some tests", () => {
        it("should test something", async () => {
            const someMockedUsers = [/* some users */];
            const mockedManager = {
                save: jest.fn()
            }
            connection.transaction.mockImplementation((cb) => {
                cb(mockedManager);
            });

            await userService.createMany(someMockedUsers);

            expect(connection.transaction).toHaveBeenCalled();
            expect(mockedManager.save).toHaveBeenCalledTimes(2);
            // ...

        });
    });


});

这篇关于如何使用 Jest 在 TypeORM 中存根 EntityManager 和 Connection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆