I'm trying to find out if I'm able to conditionally skip a test it()
in my test suite and deal with its async nature as well.
I've read about conditional testing in Cypress docs https://docs.cypress.io/guides/core-concepts/conditional-testing.html and also mochajs documentation about it https://mochajs.org/.
My intention is to check if an error is displayed on a website, and skip the test if it does. Otherwise continue with the assertions.
The code snippet from mochajs that I'm trying to take to my test in Cypress is:
it('should only test in the correct environment', function() {
if (/* check test environment */) {
// make assertions
} else {
this.skip();
}
});
So what I've got in Cypress is:
it('shows the list', function() {
if (queryFailed()) {
this.skip();
} else {
cy.get('.list')
.should('be.visible')
}
Note that I changed the arrow function in my it()
to be a function()
so that I can make use of this
.
queryFailed()
is a function that checks if the query succeeded or not.
function queryFailed() {
cy.get('.spin')
.then($container => {
const htmlLoaded = $container[0].innerHTML;
if (htmlLoaded.indexOf('List') !== -1) {
return false;
}
if (htmlLoaded.indexOf('error') !== -1) {
return true;
}
cy.wait(1000);
queryFailed();
});
}
Briefly, if the content of the div
element I'm waiting for has "error" then I know the query failed so I return true, otherwise I return false.
What I see in my tests after debugging, is that even though the condition works well, the async nature of JS executes the code in the else
statement at the same time than the if
. So the final output is as if there is no condition at all, since everything is tested.
Is there a better way of dealing with this async feature of JS?