Skip to content
Advertisement

Wait for an own function (which returns a promise) before tests are executed

I’m new to cypress and am trying to figure out how things work.

I have my own function (which calls a test controller server to reset the database). It returns a promise which completes when the DB have been successfully resetted.

JavaScript

My goal is to be able to execute it before all tests.

JavaScript

How can I do that in cypress?

Update

I’ve tried

JavaScript

But then I get an warning saying:

Cypress detected that you returned a promise in a test, but also invoked one or more cy commands inside of that promise

I’m not invoking cy in resetDb().

Advertisement

Answer

Cypress have promises (Cypress.Promise), but they are not real promises, more like duck typing. In fact, Cypress isn’t 100% compatible with real promises, they might, or might not, work.

Think of Cypress.Promise as a Task or an Action. They are executed sequentially with all other cypress commands.

To get your function into the Cypress pipeline you can use custom commands. The documentation doesn’t state it, but you can return a Cypress.Promise from them.

JavaScript

That command can then be executed from the test itself, or as in my case from before().

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement