Search code examples
javascriptangularjsinternationalizationphantomjsecmascript-intl

Javascript Internationalization API is not supported by PhantomJS


I have series of Jasmine tests running against an AngularJs service that uses ECMAScript Internationalization API. They all run successfully when I run them through Chrome. However, when I use PhantomJS to run them through maven, they all fail as it seems PhantomJs does not support Internationalization API yet.

The error message I get for the tests using Intl object is :

1: ReferenceError: Can't find variable: Intl in localizationService.js

And the rest of the tests just fail.

The tests are simple and look like this:

it('Format date with en-us locale', (function (){
    var date= "06/13/2013"
    expect(service.date(date,'en-us')).toEqual("6/13/2013");
}))

and the methods in service (localizationService.js) are simple wrappers around Intl API:

function getCurrentTimeZone(){
    return Intl.DateTimeFormat().resolved.timeZone
}

function date(dateInput,locale,options){
        // some other stuff
        // ... 
        if (locale) {
            return _date.toLocaleDateString(locale,options);
        } else {
            return _date.toLocaleDateString();
        }
}

My questions are:

1- Is my assumption correct that PhantomJS v1.9.2 does not support ECMAScript internationalization API? Is there anyway to confirm that?

2- How can I approach resolving this issue? I need to run my tests through maven and I will have more tests hitting my localizationService API one way or the other.
Thanks


Solution

  • Not sure if you're using Karma or not, but here's what I had to do to fix the same issue.

    1. npm install karma-intl-shim --save-dev

      This will also install the polyfill library Intl;

    2. Add 'intl-shim' to the frameworks collection in karma.conf.js:

      ...
      frameworks: ['intl-shim'],
      
    3. Add the locale file you wish to test with in karma.conf.js, for example 'en-US':

      ...
      files: [
            './node_modules/Intl/locale-data/jsonp/en-US.js',
      ...