Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell Jest that spaces are in fact, spaces?

Given the code:

import { getLocale } from './locale';

export const euro = (priceData: number): string => {
  const priceFormatter = new Intl.NumberFormat(getLocale(), {
    style: 'currency',
    currency: 'EUR',
  });

  return priceFormatter.format(priceData);
}

export default null;

and the related test:

import { euro } from './currency';

test('euro', () => {
  expect(euro(42)).toBe("42,00 €");
});

Jest says:

Screenshot of Jest complaining that the string are not equal even though both are seemingly the same string. Error is on the space in between number and currency produced by the Intl component.

Even if I copy-paste the expected result of Jest to my assert, the error is still the same.

So the question is: Why the hell? :-D

like image 838
Soullivaneuh Avatar asked Feb 28 '21 12:02

Soullivaneuh


People also ask

What is space?

Space is the area beyond the upper limits of Earth’s atmosphere. It is where all of the asteroids, comets, planets, stars, solar systems and galaxies in our universe are found. Space is a vacuum, meaning it contains almost nothing, but it is not completely empty. Interesting Facts about Space

What are 5 interesting facts about space?

Interesting Facts about Space Space does not begin at a specific altitude above the Earth, but the Kármán line at 100 km is a commonly used definition. The temperature in the void of space is about −270.45 °C. Space is a hard vacuum, meaning it is a void containing very little matter.

Why are there spaces between words in Japanese?

The other few places you might see spaces in Japanese are also edge cases. If something is written in romaji, spaces are used just like with English. As noted earlier, while hiragana, kanji and katakana are full-width fonts, romaji is half-width. This means spaces are necessary between words to aid in clarity.

What can I find on the Space blog?

Scientific, historical and cultural facts about space, galaxies, the planets and other objects in the solar system. The latest discoveries and interesting space related features can be found on the blog, while the gallery highlights graphics and diagrams that illustrate more aspects of the the universe.


1 Answers

You want this test to assert:

"42,00\xa0€"

It's not a space (different ascii code / unicode). According to a jest issue about string comparison being incorrect Intl.NumberFormat uses a non-breaking space.

And as pointed out in a similar question's answer:

NumberFormat use small non-breaking space (\u202f) for thousand separator and normal non-breaking space beforece currency (\xa0).

like image 50
k0pernikus Avatar answered Oct 19 '22 14:10

k0pernikus