3

I have an issue where Jasmine.js (2.4.1) is not able to test the output of toLocaleString. Given the code on jsfiddle; https://jsfiddle.net/2utf6mb3/

var options = {
   style: "currency",
   currency: "CHF"
};

var number = 1000000;
var result = number.toLocaleString("DE-CH", options); 
//"CHF 1'000'000.00" does not work with toEqual
var result = number.toLocaleString("DE-CH"); 
//"1'000'000" works with toBe() or toEqual()

I have many numbers that needs to be formatted with or without currency. The test without currency (additional options object) works fine with a "toBe" or "toEqual" matcher. But as soon as I have a currency option added, both matching options won't work. At least, for me, the "toEqual" matching should work. Am I missing something here?

Ed Michel
  • 898
  • 1
  • 11
  • 23
  • In general I don't advocate testing the output of built-in functions, but I do agree that this is very strange. – MBielski Feb 15 '16 at 16:01

1 Answers1

3

Your regex test works if you replace the space following CHF to a . (dot). result.charCodeAt(3) returns 160, which is not a space.

peterfoldi
  • 7,451
  • 5
  • 21
  • 19
  • 1
    It is actually a non-breaking space. – nicofrand Feb 16 '16 at 14:53
  • Thank you for the great response, now I have two tests for this: it has to be equal "String.fromCharCode(67, 72, 70, 160, 49, 39, 48, 48, 48, 39, 48, 48, 48, 46, 48, 48)" and it should match the given regex "/CHF.[1]'[0]+'[0]+.[0]+/g" – Ed Michel Feb 16 '16 at 15:09