Tests on jest and new Date()
There are various approaches to this, many are discussed in this thread.
Probably the most simple and clean approach in this case is to spy on Date
and get what was returned from new Date()
using mockFn.mock.instances
like this:
function foo() {
return {
key_a: "val_a",
key_b: new Date()
};
}
test('foo', () => {
const spy = jest.spyOn(global, 'Date'); // spy on Date
const result = foo(); // call foo
const date = spy.mock.instances[0]; // get what 'new Date()' returned
const expectedObject = {
key_a: "val_a",
key_b: date // use the date in the expected object
};
expect(result).toEqual(expectedObject); // SUCCESS
})