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.

function resetDatabase(){
  // returns a promise for my REST api call.
}

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

describe('Account test suite', function () {

  // how can I call resetDb here and wait for the result
  // before the tests below are invoked?

  it('can log in', function () {
        cy.visit(Cypress.config().testServerUrl + '/Account/Login/')

        cy.get('[name="UserName"]').type("admin");
        cy.get('[name="Password"]').type("123456");
        cy.get('#login-button').click();
  });

  // .. and more test

})

How can I do that in cypress?

Update

I’ve tried

  before(() => {
    return resetDb(Cypress.config().apiServerUrl);
  });

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.

Cypress.Commands.add('resetDb', function () {
  var apiServerUrl = Cypress.config().apiServerUrl;
  return new Cypress.Promise((resolve, reject) => {
    httpRequest('PUT', apiServerUrl + "/api/test/reset/")
      .then(function (data) {
        resolve();
      })
      .catch(function (err) {
        reject(err);
      });
  });
});

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

describe('Account', function () {
  before(() => {
    cy.resetDb();
  });

  it('can login', function () {
    // test code
  });

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