1

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:

  1. The .only doesn't work.
  2. 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:

  1. 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);
};
  1. 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")
  });

});
  1. 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)
  });

});
  1. Remove the code block at step 1 and see 2 test cases execute normally.
Chip Li
  • 41
  • 5
  • What are you wanting to accomplish after a test finishes and a before `after()` and `afterEach()`? – jjhelguero May 04 '22 at 14:40
  • for example: do soft assertions. If you put the assertAll() at `afterEach()` it will skip all the rest test cases if assertAll() throw error. If you put the assertAll() at the end of the test case, it will be ignored when there's a Cypress error that occurs before. – Chip Li May 05 '22 at 05:29
  • `it.only` using the monkey patch from https://stackoverflow.com/questions/55868107/does-cypress-support-soft-assertion works with Cypress 3.8.0 and thereafter it is broken. In Cypress 9.7.0 I get the error message `Cannot read properties of undefined (reading 'parent')`. I posted also about this in the original post which suggested the monkey patch. – MikeMcC399 Jul 06 '22 at 13:21
  • I found a related issue in the Cypress GitHub issue list under ["this" context doesn't work as expected in one of my repos](https://github.com/cypress-io/cypress/issues/17300). There is a comment which says: "This happens because you overwrite `it` global function but fail to preserve the context by using `=>` instead of function syntax. ... You should use the `function` syntax and be careful how you call the overwritten functions ...". I've asked if the issue submitter could post the corrected code. – MikeMcC399 Jul 20 '22 at 09:44
  • Great!!! Hope the code from the issue submitter could resolve our issue. – Chip Li Jul 20 '22 at 10:15
  • There is a suggested solution for `it.only` in github.com/cypress-io/cypress/discussions/23280. This has not yet been confirmed though. – MikeMcC399 Nov 03 '22 at 10:12
  • 1
    that's my suggestion :) I made an executable example on github: https://github.com/lehoanglu0ng/cypress-only – Chip Li Nov 03 '22 at 16:01

0 Answers0