broad-analyst-94821
02/03/2023, 10:17 AMcypress-promise to allow for use of async await actions to avoid promise hell.
It works locally but fails in beforeAll / before in the pipeline
How can I avoid promise hell? There are more complex calls that depend on the result of previous calls for the setup between tests.gray-kilobyte-89541
02/03/2023, 11:49 AMbroad-analyst-94821
02/03/2023, 12:38 PMbefore block in Electron / pipelines using headless Cypress?broad-analyst-94821
02/03/2023, 12:52 PMbefore(() => {
cy.wrap(1000).then(async () => {
await setupData();
});
});gray-kilobyte-89541
02/03/2023, 1:22 PMbroad-analyst-94821
02/03/2023, 3:00 PMbefore and beforeEach. And this is to avoid metaphorical promise hell.
The setupData function is just an async function. Each file has a series of steps to clean up the environment if data exists, and set up layers of more data.
An allegorical example:
We will quickly get into promise hell for something likebroad-analyst-94821
02/03/2023, 3:03 PMasync awaitbroad-analyst-94821
02/03/2023, 3:12 PMasyncExampleSetup fn fails in headless Cypress ..
The cy.callAPI would be wrapped in `cypress-promise`'s promisify fngray-kilobyte-89541
02/03/2023, 3:17 PMcy.callAPI is your command, and it is something that makes the call, like using cy.request, then you are overcomplicating everything (in the first variant) 🙂broad-analyst-94821
02/03/2023, 3:21 PMcy.request there (one of the top screenshots)broad-analyst-94821
02/03/2023, 3:21 PMgray-kilobyte-89541
02/03/2023, 3:49 PMlate-planet-4481
02/03/2023, 9:42 PMtask for your setup, then just run your API setup in Node.broad-analyst-94821
02/04/2023, 9:41 AMpowerful-orange-86819
02/04/2023, 10:30 AMjs
let counter = 0;
let waitTime = 500;
const someFunction = () => {
cy.request(requestName).then((response) => {
if (response.status !== 200 && counter < 10) {
cy.wait(waitTime);
someFunction();
waitTime = waitTime + 500;
counter++;
} else {
cy.request(anotherRequest).then((response) => {
expect(response.status).to.eq(200);
});
}
});
};
It will wait some time, try the request again and see if the response body or status meets the expectations and then do what you want with the body before moving forward. In a complex microservice infrastructures It can help a lotbroad-analyst-94821
02/06/2023, 7:17 AMAsync await just works, and should work with Cypressgray-kilobyte-89541
02/07/2023, 12:37 PM