I am unit testing a module which imports another function as a dependency. In the unit test, I've used the Sinon package to stub that dependency. I'm now using Sinon's calledOnceWithExactly method to check that this dependency is called exactly once, with a specific parameter passed to it. It seems like my call to calledOnceWithExactly("items") should return true, but it is returning false--Sinon seems to think the dependency was called without passing any parameters at all. Any ideas why? My code is below.
More Info
createModelStub.calledOnce is 1, confirming the function is called exactly once.calledOnceWithExactly("items") to calledOnceWithExactly(), then that returns true. As if Sinon thinks the function was called with no parameters.My Code
Test file: client/test/unit-tests.js
import test from "tape"; // assign the tape library to the variable "test"
import sinon from "sinon";
import { saveToDb } from "../src/module-save-to-db.js";
import createModel from "../../server/models/createModel.js";
test("Test of save to database.", async function (t) {
const createModelStub = sinon.stub(createModel, "createModel").returns(555);
await saveToDb();
console.log(`createModelStub.calledOnce`);// Prints 1.
console.log(createModelStub.calledOnceWithExactly("items"));// Prints false, even though createModel is indeed called once with "items" as a parameter.
t.end();
});
Module under test: client/src/module-save-to-db.js
import { createModel } from "../../server/models/createModel.js";
export function saveToDb() {
return createModel("items").then((myModel) => {// createModel called and passed "items" as a parameter.
const model = new myModel({
number: 555,
all_items: "value1"
});
return true;
});
}
Dependency module is at: server/models/createModel.js