skip to Main Content

I am trying to validate two json file and if the file data is valid then log it as "success", if it is invalid log it as "fails". But my test case tend to show fail all the time even the value present in the file is correct. I checked the values in the file by introducing logs in each function.

I tried the above stuff but i receive "fail" all the time
If there could be a solution i would be happy, i am not sure what is wrong
output

test.cy.js

cy.get('.dataTable-custom').get('.w-40').eq(0).then((el) => {
                cy.writeFile('request-number.json',
                    {
                        request_number: el.text()
                    })
            })
            cy.readFile('request-number.json').then((request) => {
                expect(request).to.be.an('object')
                cy.log(request)
                cy.readFile('application-details.json').then((json) => {
                    expect(json).to.be.an('object')
                    cy.log(json)
                    if (request === json) {
                        cy.log("success")
                    }
                    else {
                        cy.log("fail")
                    }
                })
            })```
**request-number.json**
`{
  "request_number": "ARS/0099/2023"
}`
  
**application-details.json**
`{
  "request_number": "ARS/0099/2023"
}`


2

Answers


  1. In Javascript you can’t just simply compare two objects using "==" or "===" , you need to convert the object to a stringified version first and then compare.

    ...
    if (JSON.stringify(request) === JSON.stringify(json)) {
        cy.log("success")
      }
      else {
        cy.log("fail")
     }
    ...
    

    Reference: https://www.educative.io/answers/how-to-compare-two-objects-in-javascript

    Login or Signup to reply.
  2. why not use just deep equal?

    expect(json).to.deep.equal(request)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search