question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Warn when defining alias in before hook - as they are not reusable in following tests.

See original GitHub issue

Since this is my first Issue here, I want to use this opportunity to thank you guys for your hard work. I love and enjoy working with cypress so far and I’m really looking forward to upcoming releases.

Current behaviour:

It seems like, that a defined alias can only be used once under certain circumstances. The test code will be self explanatory about my approach.

Desired behaviour:

The defined alias should be reusable as expected. Since we are doing a lot of end to end testing, we are trying hard to achieve the best possible performance. As far as I know, requery-ing the dom with cy.get() is in general a bit expensive, so we try to avoid that. Making aliases reusable as much as possible, would also result in prettier, slimmer and easier to manage test code.

Test code:

describe('reusing alias', () => {
  before(() => {
    cy.visit('/my-page.html');
  });
  // ...
  describe('number input', () => {
    before(() => {
      cy.get('#my-input').as('input');
    });
    it('should be of type number', () => {
      cy.get('@input').should('have.attr', 'type', 'number');
    });
    it('should allow a minimum value of 1', () => {
      cy.get('@input').should('have.attr', 'min', '1');
    });
    it('should allow a maximum value of 3', () => {
      cy.get('@input').should('have.attr', 'max', '3');
    });
  });
});

Additional Info (images, stack traces, etc)

The given Example will lead to the following behaviour:

  • The first it() will succeed
  • The second and third it() will fail image
  • Changing the before in describe number imput into a beforeEach will solve the problem. But not really, I don’t want to re-query the dom, I just want to re-use the defined alias.

Maybe I am getting something wrong here. If so, feel free to correct my way of thinking here.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:29
  • Comments:37 (7 by maintainers)

github_iconTop GitHub Comments

97reactions
pckhoicommented, Jan 13, 2020

Clearing aliases between each test is surprisingly inflexible for no good reason. I do much data setup in before all hook and save ids of generated objects in aliases. Doing the same setup for every tests will add 10s of seconds to each test. If your remedy is to add all related tests into one single test then you end up with ugly, long test that do too many things and hard to maintain.

Even after all hook forget all the previously set aliases. Then why have before all/after all hook at all? This is just asking for developers to circumvent the alias issue by using regular variable instead.

23reactions
ketysekcommented, Apr 1, 2020

Ach, I would love to prepare setup for my tests once in before hook. Would you consider to enable aliases? I think many users would appreciate it 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

cypress - Setting a variable in before cannot be accessed in a ...
The alias sets up a variable of the same name on the Mocha context ( this ) and it's not cleared between tests....
Read more >
Versioning and Aliases - Amazon Lex - AWS Documentation
Describes using versions and aliases with Amazon Lex. ... Before you can publish a bot, you must point it to a numbered version...
Read more >
Cypress Create the Aliases before each test - ProgramsBuzz
In this article, we will discuss how we can create an alias in before each hook and access them in the test steps....
Read more >
How To Make A Cypress Alias Work Between Test Cases
Ask questionsWarn when defining alias in before hook as they are not reusable in following tests. Since this is my first Issue here...
Read more >
Testing - Spring
These base test classes provide well-defined hooks into the ... Immediately before execution of the test method but after test setup.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found