简体   繁体   中英

How to mock Sequelize functions in Typescript

I am writing unit test cases and I have sequelize.transaction in my function. How to I mock the sequelize.transaction function.

My function looks like

create = async (req: Request) => {

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

jest.mock('sequelize', () => ({
  transaction: jest.fn(() => Promise.resolve()),
}));

describe('...', () => {
  it('should ...', () => {
    sequelize.transaction.mockImplementation(() => { ... });
    expect(sequelize.transaction).toBeCalled();
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM