Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

within does not yield the subject from previous command #2106

Closed
wadjoh opened this issue Jul 6, 2018 · 4 comments · Fixed by #8699
Closed

within does not yield the subject from previous command #2106

wadjoh opened this issue Jul 6, 2018 · 4 comments · Fixed by #8699

Comments

@wadjoh
Copy link

wadjoh commented Jul 6, 2018

Current behavior:

I am having trouble with .within() and .should(). .within() seems to yield the last subject that was yielded inside its scope rather than the subject from the previous command.

    cy.get('.query-list')
    .within(() => {
      cy.get('li.first');
    })
    // `should` should run assertion against '.query-list'.
    // Instead runs against 'li.first'
    .should('be.visible')
    // `get` should get '#query-btn'
    // Instead tries to get 'li.first #query-btn'
    .get('#query-btn');

screen shot 2018-07-06 at 9 04 37 am

The .within() command yields the li.first subject instead of .query-list.
And I'm not sure if this is a separate issue, but I found it at the same time: when I run the .get() command on #query-btn after the .should() command, it tries to find li.first #query-btn

    cy.get('.query-list')
    .within(() => {
      cy.log('inside the `within`');
    })
    // Assertion should run against '.query-list' subject
    .should('be.visible')
    .get('#query-btn');

screen shot 2018-07-06 at 9 07 09 am

In this example, I expected .should() to assert that .query-list was visible, but it instead receives null.

Desired behavior:

The docs state:

.within() yields the same subject it was given from the previous command.

But in my examples it seems to yield the last subject that was yielded from within its own scope.

Steps to reproduce:

Reproduction repo: https://github.com/hooskers/cypress-test-tiny

npm i
npm run cypress:open

Test cases are in cypress/integration/spec.js

Versions

Cypress: 3.0.2
Browser: Chrome 67.0.3396.99
OS: MacOS 10.13.5

@chrisbreiding chrisbreiding added the stage: needs investigating Someone from Cypress needs to look at this label Jul 9, 2018
@jennifer-shehane
Copy link
Member

jennifer-shehane commented Jul 12, 2018

The within is behaving the way you described - not according to what the docs say. It yields the subject of the last command within the within.

Reproducible example

it('get an el', () => {
  cy.visit('http://example.cypress.io')
  cy.get('.banner').within(($banner) => {
    console.log($banner)   // logs .banner el
    cy.get('a').first()
  }).then(($banner) => {
    console.log($banner)    // logs anchor el and not banner
  })
})

Now the question is, are the docs inaccurate? Or is the implementation inaccurate? So, we need to make a decision on what would likely be expected to be yielded from a within command.

@BrettHoutz
Copy link

Any update on this? Since within is still behaves in this way, perhaps the docs should simply be updated to describe the behavior.

@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: needs investigating Someone from Cypress needs to look at this labels Oct 2, 2020
@cypress-bot cypress-bot bot added stage: pending release There is a closed PR for this issue and removed stage: needs review The PR code is done & tested, needs review labels Oct 6, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 6, 2020

The code for this is done in cypress-io/cypress#8699, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 14, 2020

Released in 5.4.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v5.4.0, please open a new issue.

@cypress-bot cypress-bot bot removed the stage: pending release There is a closed PR for this issue label Oct 14, 2020
@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Oct 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants