Javascript Internationalization API is not supported by PhantomJS

◇◆丶佛笑我妖孽 提交于 2019-12-08 15:48:52

问题


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


回答1:


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',
    ...
    



回答2:


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

It looks like PhantomJS is based on WebKit, so it does not support the new ECMAScript internationalization API.

Even for Chrome, the API made it into V8 only recently, it is still in beeding_edge, not in main: See http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/, the i18n files (.cc, .h, .js). This means after the split from WebKit.

Here is the current status for i18n support: http://mihai-nita.net/2013/07/28/javascript-internationalization-api/

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.

If I would be the maintainer of PhantomJS I would consider going with the Google branch of WebKit, before it they diverge too much and make it too hard to catch-up. Chrome has more of the market than Safari (not and invitation for flame wars, just a personal opinion without any weight :-)

I am not familiar with PhantomJS, and I don't know what it offers, but if you can separate the JavaScript tests to run on v8 you might try using it for testing from command line. Building beeding_edge was painless, and I did it on Win, Mac OS X, and Linux without any problems.



来源:https://stackoverflow.com/questions/19228209/javascript-internationalization-api-is-not-supported-by-phantomjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!