cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Unable to assert that a dynamic alias does not exist

Open steinybot opened this issue 2 years ago • 3 comments

Current behavior

     CypressError: `cy.get()` could not find a registered alias for: `@bar`.
You have not aliased anything yet.
      at $Cy.aliasNotFoundFor (http://localhost:62327/__cypress/runner/cypress_runner.js:132892:66)
      at $Cy.getAlias (http://localhost:62327/__cypress/runner/cypress_runner.js:132835:12)
      at <unknown> (http://localhost:62327/__cypress/runner/cypress_runner.js:132429:21)
      at Object.subjectFn (http://localhost:62327/__cypress/runner/cypress_runner.js:144467:16)
      at $Cy.verifyUpcomingAssertions (http://localhost:62327/__cypress/runner/cypress_runner.js:143811:31)
      at onRetry (http://localhost:62327/__cypress/runner/cypress_runner.js:144458:15)
      at retryQuery (http://localhost:62327/__cypress/runner/cypress_runner.js:144471:10)
      at <unknown> (http://localhost:62327/__cypress/runner/cypress_runner.js:144621:17)
      at tryCatcher (http://localhost:62327/__cypress/runner/cypress_runner.js:1807:23)
      at Promise._settlePromiseFromHandler (http://localhost:62327/__cypress/runner/cypress_runner.js:1519:31)
      at Promise._settlePromise (http://localhost:62327/__cypress/runner/cypress_runner.js:1576:18)
      at Promise._settlePromiseCtx (http://localhost:62327/__cypress/runner/cypress_runner.js:1613:10)
      at _drainQueueStep (http://localhost:62327/__cypress/runner/cypress_runner.js:2411:12)
      at _drainQueue (http://localhost:62327/__cypress/runner/cypress_runner.js:2400:9)
      at Async._drainQueues (http://localhost:62327/__cypress/runner/cypress_runner.js:2416:5)
      at Async.drainQueues (http://localhost:62327/__cypress/runner/cypress_runner.js:2286:14)
  From Your Spec Code:
      at Context.eval (webpack://cypress-test-tiny/./cypress/e2e/spec.cy.js:34:11)

Desired behavior

cy.get("@bar").should("not.exist"); should pass when there is no alias registered with that name instead of erroring out.

Test code to reproduce

https://github.com/steinybot/cypress-test-tiny/tree/jason/dynamic-alias

/// <reference types="cypress" />
describe('page', () => {
    it('works', () => {
        cy.intercept(
            {
                method: "POST",
                url: "/test",
            },
            (request) => {
                request.alias = JSON.parse(request.body).name
            }
        )

        const click = () => {
            fetch(
                "/test",
                {
                    method: "POST",
                    body: '{ "name": "foo" }',
                });
        };

        cy.get('body').then((body) => {
            const button = document.createElement("button")
            button.id = "test-button"
            button.textContent = "Test"
            button.addEventListener("click", click)
            body[0].appendChild(button)
        })

        cy.get('#test-button').click()

        cy.wait("@foo").should("exist");
        cy.get("@bar").should("not.exist");
    })
})

Cypress Version

13.6.1

Node version

v21.2.0

Operating System

macOS 14.1.2 (23B92)

Debug Logs

No response

Other

No response

steinybot avatar Dec 21 '23 03:12 steinybot

Workaround is to put this in support/commands.js:

Cypress.Commands.overwriteQuery("get", function (originalFn, aliasOrSelector, options) {
        const innerFn = originalFn.call(this, aliasOrSelector, options)
        return (subject) => {
            try {
                return innerFn(subject)
            } catch (e) {
                if (typeof e.message === 'string' && e.message.startsWith("`cy.get()` could not find a registered alias for:")) {
                    return undefined
                } else {
                    throw e
                }
            }
        }
    }
)

steinybot avatar Dec 21 '23 23:12 steinybot

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

cypress-app-bot avatar Jun 19 '24 01:06 cypress-app-bot

I do not have a test case for this right now but I would be surprised if it was fixed.

steinybot avatar Jun 25 '24 04:06 steinybot