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

222
Views
How do I use the calledOnceWithExactly method on a stub?

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

  • In the test file, the value of createModelStub.calledOnce is 1, confirming the function is called exactly once.
  • If I change calledOnceWithExactly("items") to calledOnceWithExactly(), then that returns true. As if Sinon thinks the function was called with no parameters.
  • I've done this succesfully in the past, in another project, using this exact same approach. But that was maybe ~6-12 months ago by now, so maybe things changed.
  • I had a look through Sinon documentation, but couldn't see anything of note. Thanks in advance.

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

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!