Pradap Pandian
Pradap Pandian

Reputation: 406

javascript assertion between two numbers range

How do I find a assertion for a value between two numbers

let lastArrayAmountValue=50;
assert.equal(lastArrayAmountValue, '5');

I want to assert whether the number is between 5-10 This is the test am doing on cypress.

Upvotes: 1

Views: 807

Answers (3)

TesterDick
TesterDick

Reputation: 10550

If you want to apply within to an asynchronous element, move it into .should() to trigger retry of the assertion.

For example,

cy.get(elementArraySelector)
  .last()
  .should($el => {
    const value = +$el.text() || 0; 
    expect(lastArrayAmountValue).to.be.within(5,10)  // retry until timeout
  })

Upvotes: 2

Alapan Das
Alapan Das

Reputation: 18626

You can also do like this. Number is greater than equal to 5 and less than equal to 10.

cy.wrap(lastArrayAmountValue).should('be.gte', 5).and('be.lte', 10)

Upvotes: 1

davidhu
davidhu

Reputation: 10462

expect(lastArrayAmountValue).to.be.within(5,10)

https://docs.cypress.io/guides/references/assertions#BDD-Assertions

looks like that ability is built into cypress

Upvotes: 2

Related Questions