javascriptjquerytypescriptcypressshould.js

should.have.property to verify key-value pair from object


I would like to check if key-value pair exist on a object. I use should.js with syntax: cy.gey(selector).should('have.property', 'Compasny Feature: ', ['open space ']); but I get this error Error: AssertionError: expected { Object (Company Feature: , Name: ) } to have property 'Company Feature: ' of [ 'open space ' ], but got [ 'open space ' ] I totally have no idea which part the value does not match. or it is just a bug from should.js?

// debug output
{
    "Company Feature: ": [
        "open space "
    ],
    "Name: ": [
        "John, Amazon "
    ]
}
// test.ts
cy.gey(selector).should('have.property', 'Compasny Feature: ', ['open space ']);



// html
<div class="container">
    <span>
        <span class="title">Company Feature: </span>
        <span class="text">open space </span>
    </span>
    <span>
        <span class="title">Name: </span>
        <span class="text">John, Amazon </span>
    </span>
</div>

Solution

  • The issue is simply that you are referencing different instances of the array. You can fix this by referencing the same instance of the array like so:

    const testFeature = [
            "open space "
        ];
    const TestData = {
        "Company Feature: ": testFeature,
        "Name: ": [
            "John, Amazon "
        ]
    }
    // test.ts
    cy.gey(selector).should('have.property', 'Company Feature: ', testFeature);
    

    Alternatively you can use .deep in the chain and have it check against the structure see docs: https://www.chaijs.com/api/bdd/#method_property

    // test.ts
    cy.gey(selector).should('have.deep.property', 'Company Feature: ', testFeature);