Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!