Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda