This feels like it's nowhere near as difficult as I'm making it. Here is a little (heavily simplified) function as an example:
dateCalculator.js
const today = new Date();
const deliveryDate = new Date();
deliveryDate.setDate(today.getDate() + 3);
export const dateCalculator = () => {
return `${deliveryDate.toLocaleString('en-GB', { weekday: 'long' })}`;
};
So this function grabs today's date, adds three days to it and then returns the day of the week. Obvs, I'm going to test it:
dateCalculator.spec.js
import { dateCalculator } from './dateCalculator';
describe('dateCalculator', () => {
it('should return the correct day of the week', () => {
expect(dateCalculator()).toBe('Thursday');
});
});
This test passes, but it will only pass when it is run on a Monday.
How do I force the variable today
to be a specific date which I can control from the test? I can't work out how to mock this single variable.