I need to verify that all transaction calls are succeeded. Here's the example:
module.js
const functionUnderTest = async (helper) => {
await helper.transaction(async (transaction) => {
await helper.doSomething();
await helper.doSomething2();
await helper.expectedToBeCalled();
console.log("done");
});
};
module.exports = {
functionUnderTest,
};
The problem comes, that for some reason with sinon I cannot verify that the last call was done at all, however the message "done" is always printed.
Here how output looks like
$ npm run test
> sinon-test@1.0.0 test
> mocha
module
✔ the former function should be called
done
1) the latter function should be called
done
1 passing (7ms)
1 failing
1) module
the latter function should be called:
AssertionError: expected stub to have been called at least once, but it was never called
at Context.<anonymous> (test/module.test.js:31:48)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
The tests itself are represented below
helpers/chai.js
const chaiAsPromised = require("chai-as-promised");
const chaiSinon = require("sinon-chai");
chai.use(chaiSinon);
chai.use(chaiAsPromised);
module.exports = { expect: chai.expect };
test/module.test.js
const { expect } = require("./helpers/chai");
const sinon = require("sinon");
const { functionUnderTest } = require("../module");
describe("module", function () {
let sandbox;
let mockHelper;
const transaction = {};
beforeEach(function () {
sandbox = sinon.createSandbox();
mockHelper = {
transaction: sandbox.stub().callsArgWithAsync(0, transaction),
doSomething: sandbox.stub().resolves({}),
doSomething2: sandbox.stub().resolves({}),
expectedToBeCalled: sandbox.stub().resolves({}),
};
});
afterEach(function () {
sandbox.restore();
});
it("the former function should be called", async function () {
await functionUnderTest(mockHelper);
expect(mockHelper.doSomething).to.be.called;
});
it("the latter function should be called", async function () {
await functionUnderTest(mockHelper);
expect(mockHelper.expectedToBeCalled).to.be.called;
});
});
To make it complete and easy to test, I've created a repo that you can play with: https://github.com/vichugunov/sinon-test
My question are:
- how is it possible?
- what am I doing wrong?
P.S. If the call await helper.doSomething2() is commented out, tests are passing