Mocking the constructor function only in Javascript (using spyOn)

560 views Asked by At

I'm doing something like this question here which allows me to provide my own implementation of the Date constructor in my test

spyOn(window, 'Date').andCallFake(function() {
  return SOME_DATE;
});

I've now run into an issue where I want to upgrade moment.js and they're using additional Date functions in the code that I am calling with my mocked Date object. These functions don't exist:

Date.UTC() // doesn't exist in mocked date when accessed in moment.js

So I think what I need to do is somehow mock only the constructor function of Date and let the other functions off of it pass through.

I've tried various forms of .andCallThrough as well as assigning the functions that it needs to the spy object to no avail (e.g., spy.UTC = OldDate.UTC). Any ideas?

0

There are 0 answers