cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.72k stars 3.16k forks source link

Yield context to test functions #8977

Closed emilong closed 3 years ago

emilong commented 3 years ago

What would you like?

Yield context as the first argument to a test function, much like this is bound to non-arrow functions function() {}.

e.g.

it("uses this to get aliases", function() {
  console.log(this.myAlias);
});

it("uses context argument to get aliases", context => {
  // context is the same thing that would have been bound
  // to `this` if the test body was not an arrow function.
  console.log(context.myAlias); 
});

Why is this needed?

Currently the only way to get the value of aliases is to use:

  1. a function() {} style test body and use this.myAlias. Many people prefer not to use this and/or function syntax as they find it less descriptive.
  2. cy.get("@myAlias"), which has the potential to introduce a lot of nesting tests with a lot of aliases.

I believe part of this issue (related to aliases, but not to selectors) covers these concerns: https://github.com/cypress-io/cypress/issues/1417

Given that nothing appears(?) to be passed as an argument to test functions now, I think this should be fully backwards compatible.

jennifer-shehane commented 3 years ago

A done callback is passed as the first argument for test functions, same as Mocha: https://mochajs.org/#asynchronous-code So, it could not be available as the first arg.

Since this functionality is already possible with the existing arrow function and this calls, I would not expect this to be introduced anytime soon, if at all.

We will reconsider if we see this issue gains more 👍 or comments in support.