Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

295
Views
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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!