【问题标题】:How to mock Sequelize functions in Typescript如何在 Typescript 中模拟 Sequelize 函数
【发布时间】:2020-11-12 18:08:56
【问题描述】:

我正在编写单元测试用例,我的函数中有 sequelize.transaction。如何模拟 sequelize.transaction 函数。

我的函数看起来像

create = async (req: Request) => {

await sequelize.transaction(async (t) => {
  // Lock Test Balance Row
  const testBalance = await this.testBalanceDAL.findByTestTypeAndUserIdForUpdate(
    testTypeId,
    userId,
    t
  );.....`

【问题讨论】:

    标签: javascript node.js typescript mocha.js chai


    【解决方案1】:
    const sequelize = require('sequelize');
    
    jest.mock('sequelize', () => ({
      transaction: jest.fn(() => Promise.resolve()),
    }));
    
    describe('...', () => {
      it('should ...', () => {
        sequelize.transaction.mockImplementation(() => { ... });
        expect(sequelize.transaction).toBeCalled();
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2021-01-31
      • 2021-12-03
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 2018-10-20
      • 2020-02-04
      • 2017-07-12
      • 2021-02-15
      相关资源
      最近更新 更多