Mock Timezone in PhantomJS for Jasmine Test

Can I make fun of the time zone in Jasmine to check the date object?

I have a function that takes a UTC time string and converts it to a date object.

Using "2016-01-16T07: 29: 59 + 0000", I want to check that when we are in PST, we observe 2016-01-15 15:29:59 as a local date / time

I would like to be able to switch this time zone to GMT, and then make sure that we observe 2016-01-16 07:29:59 as a local date / time

(How) is this possible? (I run the Jasmine spec via Grunt using phantomjs)

My function for reference:

utcDateStringToDateObject: function(dateString){ return dateString.indexOf('+')>-1 ? new Date(dateString.split('+')[0]) : new Date(dateString); } 
+6
source share
1 answer

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'); }); 
0
source

Source: https://habr.com/ru/post/1241222/


All Articles