The Zuidz
The Zuidz

Reputation: 698

timezoneJS will not resolve time zones in unit test

I'm unit testing an AngularJS application using Jasmine and Karma. The unit test I'm currently working on is a date service that will parse strings into dates and return a timezoneJS object. I've included the date.js library in my karma.config file and the zoneFileBasePath was also defined in the main application page of my SPA. All of the angular application files have been included in my karma.config file as well.

The first error I was getting was this:

Please define a base path to your zone file directory -- timezoneJS.timezone.zoneFileBasePath.

So I included the following into my unit test:

 timezoneJS.timezone.zoneFileBasePath = 'app/tz'

 timezoneJS.timezone.init({async:false})

I did not understand why this was necessary is it is already defined, but it worked nonetheless.

I'm now getting this error

Error: Timezone "America/Chicago" is either incorrect, or not loaded in the timezone registry.

I've found a couple of posts on this issue and one recommendation I found that seemed to work for someone else was to turn off async. I've done this but it is still not working.

Timezone directory contents:

enter image description here

As you can see, it is located within my app directory, which I have specified at the zoneFileBasePath declaration. My tests are located on the same plane as app within a testing folder which are further divided into specs and mocks etc.

I need to be able to test this date service, and this problem continues to persist no matter how I try to structure the code. If anyone could help me out I would greatly appreciate it. Thanks.

Upvotes: 2

Views: 1064

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241430

TimezoneJS requires a transport layer to retrieve the time zone data files.

By default, it will work with jQuery, Zepto, or Fleegix. You could probably just include jQuery in your unit test and it would work.

I do believe you are correct in setting async:false when using jQuery.

You should take a look at this project, which ties TimezoneJS directly into Angular. It may be easier for you to consume.

You might also be interested in one of the other time zone libraries, which use the same source data but have different implementations. I list them here.

Upvotes: 2

Related Questions