I use jasmine 3.4.0, one solution is to use a clock
beforeEach(() => { jasmine.clock().install(); // whenever new Date() occurs return the date below jasmine.clock().mockDate(new Date('2024-04-08T06:40:00')); }); afterEach(() => { jasmine.clock().uninstall(); });
However, since my tests included time zones, I had to monitor my service
it('should support work', () => { const mocked = moment.tz('2019-03-27 10:00:00', 'America/New_York'); spyOn(spectator.service, 'getMoment').and.returnValue(mocked); const output = spectator.service.foo('bar'); // My asserts expect(output.start.format(dateFormat)).toBe('2019-03-17T00:00:00-04:00'); expect(output.end.format(dateFormat)).toBe('2019-03-23T23:59:59-04:00'); });
source share