Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
How to expect a float number or just a number in a React test

I have the following code:

it('should return a number', async () => {
  const exchangeRate = await getExchangeRate('USD','HUF')
  expect(exchangeRate).toBe()
})

How can I build this test so that I can use the expect() method to check if the exchangeRate const is a float number or just a number ? ....

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can assert the type of the value by using typeof:

expect(typeof exchangeRate === 'number').toBe(true)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can assert on the following

   expect(Number.isInteger(exchangeRate)).toBe(true); // This will be true if its a Integer else it will be false for float.

To further add some more checks you can do the following:

  expect(!isNaN(exchangeRate) && Number.isInteger(exchangeRate)).toBe(true); // Check for Integer
  expect(!isNaN(exchangeRate) && !Number.isInteger(exchangeRate)).toBe(true); // Check for Float
about 4 years ago · Juan Pablo Isaza Relatório

0

In javascript there is only 1 numeric type: number. Both int (whole) values and float (fractional) values are stored using the number type.

It is possible to check if a number is a whole value by using Number.isInteger(x), however this will return true also if your float happens to be 3.00000.... - which is a totally valid float value, while also being exactly the same as the "integer" value 3.

In this case you probably would want to use typeof x == 'number' && !isNaN(x) which will return true for all "float" values, inclusing for those values that happen to be "integer" values.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda