esmock with sinon and checking if a function is calledOnce

56 views Asked by At

I'm trying to update a project to use import / export and that's lead me to using esmock for mocking responses in my unit tests. I used to have this code which I could then check with calledOnce & this worked when using require and sinon.stub().

mapCountAllTeams = sinon.stub(adminJS, 'mapCountAllTeams').returns(false);

Now I've updated it to esmock like this because of issues stubbing ES modules:

const adminJS = await esmock('../adminFunctions.js', {
     '../adminFunctions.js': {
        mapCountAllTeams: false
     }
});

But I don't seem to be able to call adminJS.mapCountAllTeams() from within the test, it keeps returning undefined or the below error. However when logging the actual functions output it is returning the correctly mocked value.

Promise {
  <rejected> TypeError: Cannot convert undefined or null to object
      at Function.values (<anonymous>)
      at Module.mapCountAllTeams (file:///C:/functions/adminFunctions.js?esmk=1?esmk=1:285:29)
      at Context.<anonymous> (file:///C:/functions/test/fklProdAdmin.test.js:103:36)

what is the best practice to be able to use esmock with calledOnce?

0

There are 0 answers