lundi 30 mars 2020

Mocking constructors using node and chai/sinon

I have a function like this

function buildToSend(repo) {
  const {
    name, ...data
  } = repo;
  return {
    msg: {
      application: data.name,
      date: new Date(),
    },
  };
}

And I need to test it but i really can't find out how to mock/stub the new Date() constructor.

Any ideas?

I already tried somethings like this but it didn't work.

    const date = new Date();
    const myStub = sinon.stub(Date.prototype, 'constructor').returns(date);
    const input = {
      name: 'name',
    };
    expect(utils.buildToSend(input)).to.deep.equal({msg: {name: 'name', date: 'THE DATE'}});

I'm missing something but i really don't know what. (of course, date is not getting called that way)

Aucun commentaire:

Enregistrer un commentaire