简体   繁体   中英

How to remove all cy.route from beforeEach in Cypress

I'm creating a lot of tests for application in my firm. Before each test I need to create a state to work on it, and it's always the same, so I created some routes in my own method and then in support/index.js file I created beforeEach that looks like this

beforeEach(() => {
    cy.server();
    cy.mockSearches(SHORTEN_SEARCHES); // this only creates mocks
    cy.loginAdmin();
});

And in 99% percent of tests it's working fine, but there is one test, that needs to work on real data. What should I do? Is there a way to ignore global beforeEach? I guess I can move this part of code to each test before each, but that's code repetition? Or maybe I should override this cy.route with empty responses?

You can add a condition to your beforeEach() to exit before your setup:

beforeEach(() => {
    if (shouldUseRealData) return;
    cy.server();
    cy.mockSearches(SHORTEN_SEARCHES); // this only creates mocks
    cy.loginAdmin();
});

As stated in the docs about environment variables , you can set an environment variable in different ways. One way would be to set it in your command line when calling cypress run:

cypress run --env use_mock=true

And then you would use it with Cypress.env('use_mock') .

beforeEach(() => {
    if (Cypress.env('use_mock')) {
        cy.server();
        cy.mockSearches(SHORTEN_SEARCHES); // this only creates mocks
        cy.loginAdmin();
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM