I was inspired by this to do the things I need after the test is finished which run before the after
and afterEach
hook. But there're 2 problems:
- The
.only
doesn't work. - The
this
doesn't work also.
As you may know, if there's any error occurs at afterEach
, Cypress will skip the rest test cases. Please advise me :)
Steps to reproduce:
- Copy this to
cypress/support/index.js
// monkey-patch `it` callback so we insert `cy.then()` as a last command
// to each test case
function itCallback(title, func) {
func();
cy.then(() => {
//do my stuffs here
});
}
const origIt = window.it;
window.it = (title, func) => {
origIt(title, func && (() => itCallback(title, func)));
};
window.it.only = (title, func) => {
origIt.only(title, func && (() => itCallback(title, func)));
};
window.it.skip = (title, func) => {
origIt.skip(title, func);
};
- Run this test case to see
.only
error:
/// <reference types="cypress" />
context('Test', () => {
it.only('Should run this only.', () => {
cy.log("only")
});
it('Should skip this.', () => {
cy.log("skip")
});
});
- Run this test case to see
this
is undefined:
/// <reference types="cypress" />
context('Test', () => {
before(() => {
cy.wrap("test").as("test");
})
it('test', function () {
cy.log('' + this.test)
});
});
- Remove the code block at step 1 and see 2 test cases execute normally.