Syed Ali
Syed Ali

Reputation: 1897

cypress test not matching expected values

A couple of tests started to fail recently while running cypress with Chrome.

Test 1

Cypress fails to verify the text correctly. Here are the screenshots:

Text displayed on the UI enter image description here

Text matched: enter image description here

Here is how the number is generated in the code:

let dateString = '';
before(() => {
  dateString = Date.now();
  timestamp = `XXX.${dateString}`;
});

it('test to save data', () => {
  // uses timestamp variable to save data
});

it('test to check data', () => {
  // uses the same timestamp variable to check data
});

How can the value of timestamp variable differ between the two tests? Is the before method called again for each it block? If it is called again then how can I ensure both tests have the same value for the variable timestamp?

Upvotes: 1

Views: 283

Answers (1)

Fody
Fody

Reputation: 31924

In this block dateString is not string it's a number, and the error looks like a rounding problem (last digit differs).

let dateString = '';
before(() => {
  dateString = Date.now();
  timeStamp = 'XXX.' + `${dateString}`;
})

Making dateString a proper string should solve the problem, as the characters will be locked in.

let dateString = '';
before(() => {
  dateString = Date.now().toString()
  timeStamp = `XXX.${dateString}`      // BTW this is how string interpolation looks
})

Upvotes: 2

Related Questions