Search code examples
javascriptjasminelocale

JavaScript: toLocaleString() testing with jasmine


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?


Solution

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