'How to mock a Sequelize transaction with Sinon?

I'm testing a function that creates a resource using Sequelize, I'm adding transactions because I want to commit everything if there are no errors, I have this

const createResource = async (resource) => {
  const t = await sequelize.transaction();
  try {

    const createdResource = await Resource.create(resource, { transaction: t });

    await t.commit();
    return createdResource;
    
  } catch (err) {
    await t.rollback();
    throw err;
  }
};

The unit test for the function above, looks like this

it('Should create a resource with no errors', function () {
    const dummy = aDummy();
    const createFake = sinon.fake.resolves({ dataValues: dummy });
    sinon.replace(Resource, 'create', createFake);
    return ResourceService.createResource(dummy)
        .then(res => {
            expect(res).to.not.be.undefined;
            expect(createFake.calledWith(dummy));
            expect(createFake.calledOnce).to.be.true;
        });
});

What I want is not to depend on any infrastructure at all, so my problem is that I don't know how to mock a Sequelize transaction with Sinon



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source