Reputation: 9804
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));
});
});
Upvotes: 1
Views: 2104
Reputation: 18079
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
})
}
Upvotes: 3