Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress API response is Array but assertion fails

Tags:

chai

cypress

I am currently trying to assert the items in an array to be true. This is how the test is currently written.

    it.only('GET getUserPermissions', () => {

        cy.request({
            method: 'get',
            failOnStatusCode: false,
            log: true,
            url: 'https://someurl.org/api/getUserPermissions?=1234',
            headers: {
                //'accept': 'application/json',
                'authorization': 'Bearer ' + login.jwt
            },
            response: []
        }).then((response) => {
            console.log(response.body)
            assert.equal(response.status, 200)
            expect(response.body).to.not.be.null
            console.log(response)
            expect(response.body).to.contain(
                [[[43239,"admin",136,4],[43257,"database.events",165,4],43258,"deadbeat.list",113,4],43266,"navigation",6,4],[43267,"object",19,4],[43272,"qa.object",31,4],43278,"user",5,4]])
        })
    })

The response I get in the Cypress runner looks like this:

TEST
1 REQUEST GET 200 https://https://someurl.org/api/getUserPermissions?=1234
2 -ASSERT expected 200 to equal 200  --Pass
3 -ASSERT expected [ Array(7) ] not to be null --Pass
4 -ASSERT expected [ Array(7) ] to include [[[43239,"admin",136,4],[43257,"database.events",165,4],43258,"deadbeat.list",113,4],43266,"navigation",6,4],[43267,"object",19,4],[43272,"qa.object",31,4],43278,"user",5,4]]  --Fails

When I test this endpoint in Postman, I get just the array [], but when I run this in the runner, the assertion fails, it's not returning the array in the same manner. How do I parse the response to check the values? These are user permissions and I need to ensure that the user has the correct permissions for the correct object.

like image 797
Xgirl360 Avatar asked Feb 11 '26 05:02

Xgirl360


1 Answers

One workaround is you can try to assert the type like

  expect(arrayname).to.be.a('array')

where 'array' is nothing but the type. In our case, we will be using response.body in the place of arrayname.

Similarly, we can use array as the type

Try this:

 it.only('GET getUserPermissions', () => {

    cy.request({
        method: 'get',
        failOnStatusCode: false,
        log: true,
        url: 'https://someurl.org/api/getUserPermissions?=1234',
        headers: {
            //'accept': 'application/json',
            'authorization': 'Bearer ' + login.jwt
        },
        response: []
    }).then((response) => {
        console.log(response.body)
        assert.equal(response.status, 200)
        expect(response.body).to.not.be.null
        console.log(response)
        expect(response.body).to.be.a('array')
    })
})
like image 134
Steffi Keran Rani J Avatar answered Feb 18 '26 16:02

Steffi Keran Rani J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!