7

I would have expected (10000).toLocaleString('de-DE') to return "10.000" but instead I get "10000".

Is there a reason this is not supported? Are there better ways to format numbers?

Ibti
  • 101
  • 7
  • I get `'10,000'` :) – daymosik Jul 24 '16 at 21:25
  • It works if i call it in a function and it gives the expected output but it didn't work for me when i tried to test it like that: `it('test toLocaleString', () => { let nb = 1000; let str = nb.toLocaleString('de-DE'); expect(str).toBe('1.000'); })` – Ibti Jul 24 '16 at 21:31
  • I think You should change locale of the browser. Try this http://stackoverflow.com/questions/22981929/how-to-set-the-browser-language-of-phantomjs – daymosik Jul 24 '16 at 21:32
  • I tried that, it didn't work. – Ibti Jul 24 '16 at 21:56
  • @Artjom B. , thank you, i know but i forget to add that. – Ibti Jul 26 '16 at 08:27
  • This seems to be a limitation of PhantomJS. Every browser handles things differently. If you want to support PhantomJS (why?) then you need to add a shim in the site you're working on so that the unit test succeeds. If PhantomJS is only the means to run some unit tests, then you need to think about, why such a unit test is important and whether you really need to keep it.. – Artjom B. Jul 26 '16 at 21:26

1 Answers1

1

It is a webkit issue, and PhantomJS doesn't want to maintain internationalization... so unfortunately we are stuck with this for some undisclosed time.

https://github.com/ariya/phantomjs/issues/12581

What I ended up doing is writing a custom matcher that checks for both, since I run in Chrome and PhantomJS.

jasmine.addMatchers({
    isAnyOf: (util, customEqualityTesters) => {
      return {
        compare: (actual, expected) => {
          let result = {};
          for (let expect of expected) {
            console.log(actual == expect);
            if (expect == actual) {
              result.pass = true;
              return result;
            }
          }
          result.pass = false
          return result;
        }
      }
    }
  })

Then you can use it like

expect(actual).isAnyOf(['10000', '10.000']);
Dinny
  • 59
  • 6