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

Can't use variables in Command callback if options exist #1089

Closed
Amdingo opened this issue Dec 18, 2017 · 2 comments
Closed

Can't use variables in Command callback if options exist #1089

Amdingo opened this issue Dec 18, 2017 · 2 comments
Labels
stage: wontfix Cypress does not regard this as an issue or will not implement this feature type: question

Comments

@Amdingo
Copy link

Amdingo commented Dec 18, 2017

  • Operating System: Win10
  • Cypress Version:
  • Browser Version:

Is this a Feature or Bug?

Bug

Current behavior:

variables in Command callback are ignored when {prevSubject: <options>} exists
i.e.:

Cypress.Commands.add('clickNavLink', { prevSubject: 'optional' }, (name) => {
  cy.get(`[data-test="${name}-nav-link"]`).click()
});

cy.clickNavLink('sales') returns GET [data-test="undefined-home-tile"]

but

Cypress.Commands.add('clickNavLink', (name) => {
  cy.get(`[data-test="${name}-nav-link"]`).click()
});

cy.clickNavLink('sales') returns GET [data-test="sales-home-tile"]

Desired behavior:

variables in a callback are respected when prevSubject is present

How to reproduce:

create a Cypress.Command with prevSubject: 'optional' variable and variable in callback.

Test code:

./support/commands.js

Cypress.Commands.add('testClickNavLink', { prevSubject: 'optional' }, (name) => {
  cy.get(`a[href="/commands/${name}"]`).click()
});

./integration/test_example_links.js

describe('Run through traversal links', function() {
  beforeEach(() => cy.visit('https://example.cypress.io'));

  ['querying', 'traversal', 'actions'].forEach((n) => {
    it('fails', () => {
    cy.testClickNavLink(n);
    })
  })
});

I know the test will fail because there's multiple, but really you just need to see that it returns undefined instead of the variable passed into the callback.

I'll throw a repo together tonight if needed but this is pretty straightforward to test.

Additional Info (images, stack traces, etc)

@brian-mann
Copy link
Member

This is not a bug, it's the correct implementation.

Setting the prevSubject to optional will always yield you the current subject as the first argument (because it may or may not exist).

The second argument will be what you invoked the command with.

https://docs.cypress.io/api/cypress-api/custom-commands.html#Dual-Commands

@Amdingo
Copy link
Author

Amdingo commented Dec 19, 2017 via email

@jennifer-shehane jennifer-shehane added stage: wontfix Cypress does not regard this as an issue or will not implement this feature type: question labels Dec 19, 2017
@brian-mann brian-mann modified the milestone: 1.4.0 Dec 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: wontfix Cypress does not regard this as an issue or will not implement this feature type: question
Projects
None yet
Development

No branches or pull requests

3 participants