Home > Enterprise >  Cypress: how can I compare the json in a downloaded file to a graphql(database) query?
Cypress: how can I compare the json in a downloaded file to a graphql(database) query?

Time:04-20

For a test I need to be able to compare the values within a downloaded json file to the values that come from a graphql query. This will then enable me to verify the values on the downloaded file are correct.

So far I have been able to make the retrieve the data from the json file and make the db call and both are visible and matching when console logged as you can see below: enter image description here

enter image description here

However, when I compare them and change the values to be incorrect in my assertions the assertions still seem to be passing. The code for the checks is below:

 const downloadsFolder = Cypress.config('downloadsFolder');
    cy.readFile(path.join(downloadsFolder, 'test.json'), { timeout: 30000 })
      .should('exist')
      .then((data) => {
        cy.graphQlQuery(url, getUserReport())
          .then((response: any) => {
            console.log(response);
            console.log(data);
            expect(response.status).to.eq(200);
            expect(data.length === response.body.data.workforce_roles.length);
            expect(JSON.stringify(data[0]) === JSON.stringify(response.body.data.workforce_roles));
            debugger;
          });

      });

Can anyone tell me what is wrong with this code and how I can go about fixing it so that when these values don't match I correctly receive an error. I suspect it is something to do with the fact cypress is not waiting for the checks to finish before moving on but I am not sure how to rectify that given cypress' troubled relationship with async/await.

CodePudding user response:

You need to qualify the last two expects. Cypress does not recognise them as they are

expect(data.length === response.body.data.workforce_roles.length).to.eq(true)
//or
expect(data.length).to.eq(response.body.data.workforce_roles.length)
expect(JSON.stringify(data[0]) === JSON.stringify(response.body.data.workforce_roles)).to.eq(true)
//or
expect(data[0]).to.deep.eq(response.body.data.workforce_roles)
  • Related