Assert request properties with cypress
Created Jun 11 2020
JS1it('Calls the correct endpoint with the expected payload when clicking on submit', () => {2cy.server();3cy.route({4url: 'https://my-awesome-app.com/submit',5method: 'POST',6}).as('submitPOST');78/**9* Use a data-testid attribute toi target the element we want to click on10* Here we want to click the submit button with the data-testid11* "submit-button"12*/13cy.get('[data-testid="submit-button"]').click();1415/**16* Wait for the submitPOST request to resolve before moving to the next command17*18* This will yield an object containing the HTTP request and response properties.19*20*/21cy.wait('@submitPOST').should(req => {22/**23* You can run any kind of assertions against the request object24*/25expect(req.method).to.equal('POST');26/**27* You can check the type of any property28*/29expect(req.request.body)30.to.have.property('foo')31.and.be.a('array');32expect(req.request.body)33.to.have.property('hello')34.and.be.a('string');3536/**37* You can run assertions on any fields38*/39expect(req.request.body.foo).to.have.members(['bar', 'baz']);40expect(req.request.body.hello).to.equal('world');41});4243/**44* This won't be checked until the @submitPOST request has resoilved45*/46cy.get('[data-testid="foobar"]');47});