How can I make `.only` and `this` to work after hacking the Cypress hook?

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.
How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum