Home  >  Article  >  Web Front-end  >  Validating String in Jest

Validating String in Jest

王林
王林Original
2024-08-09 07:08:32465browse

Hi commiters!

Perhaps you have encountered a problem with validating strings in Jest.

Validating String in Jest



For example, the test above validates that the function receives a value and returns a rounded value.

it('It should properly format a unit value with rounding.', () => {
    expect(formatCurrency(1234.56, true, false)).toEqual({
      measure: 'thousand',
      value: 'R$ 1.200,00',
    });
  });

In this test, Jest returned an error, highlighting the differences between the expected and received values, but they are the same ?

- Expected  - 1
+ Received  + 1

  Object {
    "measure": "thousand",
-   "value": "R$ 1.200,00",
+   "value": "R$ 1.200,00",

The solution is to add xa0. The problem is not with your string, but how Jest compares string values ?

The corrected code is shown above.

it('It should properly format a unit value with rounding.', () => {
    expect(formatCurrency(1234.56, true, false)).toEqual({
      measure: 'thousand',
      value: 'R$\xa01.200,00', // remove space and add \xa0
    });
  });

The above is the detailed content of Validating String in Jest. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn