1

I'm trying to make an HTTP request on my server and check the result after. Currently, I well have a result in the response object of the postCar method. But the result is null on the test on the then() method.

Where is the problem ?

postCar($access_token, $body) {
        return new Cypress.Promise((resolve) => {
            let bodyString = '';
            cy.request({
                method: 'POST',
                url: '/applications',
                form: true,
                failOnStatusCode: false,
                body: $body,
                headers: {
                    Content: 'application/json',
                    Authorization: 'Bearer ' + $access_token
                }
            })
            .then((response) => {
                cy.log('Response = ' + JSON.stringify(response));
                resolve(response);
            });
        });
    }

My test:

it('Car Spec : create a car', function () {

    let accessToken = Cypress.env('ACCESS_TOKEN')
    var correct_body = getValidBody();

    cy.postCar(accessToken, correct_body).then(function(res) {
       cy.log('Application Spec : postCar response :' + JSON.stringify(res));
    });

});
wawanopoulos
  • 9,614
  • 31
  • 111
  • 166
  • so `cy.log('Response = ' + JSON.stringify(response));` outputs `Response = null`? – Jaromanda X Apr 12 '19 at 09:03
  • Looking at the docs, can you not just `return cy.request(...` ? Wrapping it all in a promise and then resolving the response seems overkill. I might be missing something though – dbramwell Apr 12 '19 at 09:10
  • @JaromandaX No, that's `cy.log('Application Spec : postCar response :' + JSON.stringify(res));` which is null – wawanopoulos Apr 12 '19 at 09:13
  • @dbramwell Could you please provide an example ? – wawanopoulos Apr 12 '19 at 09:14
  • so `cy.log('Response = ' + JSON.stringify(response));` outputs a value – Jaromanda X Apr 12 '19 at 09:15
  • @JaromandaX Yes – wawanopoulos Apr 12 '19 at 09:16
  • I've only used cypress a little, so this isn't worth of being an actual answer but it seems to me that in the postCar function you can just have: postCar($access_token, $body) { return cy.request({ method: 'POST', url: '/applications', form: true, failOnStatusCode: false, body: $body, headers: { Content: 'application/json', Authorization: 'Bearer ' + $access_token } }) } – dbramwell Apr 12 '19 at 09:18
  • if you do what @dbramwell suggested, what happens then – Jaromanda X Apr 12 '19 at 09:18
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Apr 12 '19 at 11:41

1 Answers1

3

You shouldn't wrap the command in a Promise. Cypress Custom commands are not meant to return promises. Try just this:

function postCar ($access_token, $body) {

    let bodyString = ''

    cy.request({
      method: 'POST',
      url: '/applications',
      form: true,
      failOnStatusCode: false,
      body: $body,
      headers: {
        Content: 'application/json',
        Authorization: `Bearer ${$access_token}`,
      },
    })
    .then((response) => {
      cy.log(`Response = ${JSON.stringify(response)}`)
      return response
    })
}
kuceb
  • 16,573
  • 7
  • 42
  • 56