Skip to content

Async tests with after hook results in error #31766

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

Open
chrisbreiding opened this issue May 21, 2025 · 1 comment
Open

Async tests with after hook results in error #31766

chrisbreiding opened this issue May 21, 2025 · 1 comment

Comments

@chrisbreiding
Copy link
Contributor

Current behavior

The combination of an async test with a command like cy.request() in addition to an afterEach or after hook with any Cypress command results in the following error:

Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

The command that returned the promise was:

> cy.request()

The cy command you invoked inside the promise was:

> cy.log()

Because Cypress commands are already promise-like, you don't need to wrap them or return your own promise.

Cypress will resolve your command with whatever the final Cypress command yields.

The reason this is an error instead of a warning is because Cypress internally queues commands serially whereas Promises execute as soon as they are invoked. Attempting to reconcile this would prevent Cypress from ever resolving.

Because this error occurred during a after each hook we are skipping the remaining tests in the current suite: async test + request repro

The test passes if the afterEach hook is removed. It also passes if the async is removed from the test. This case should not error at all or at least the error message should be improved to indicate the test should not be async. It took a while to realize the async was what was causing the error.

Desired behavior

Either tests run without erroring when they are async and there is an afterEach or at least the error message is improved to indicate that async tests are not supported.

Test code to reproduce

describe('async test + request repro', () => {
  afterEach(() => {
    cy.log('afterEach')
  })

  it('cy.request()', async () => {
    cy.request('https://jsonplaceholder.cypress.io/todos/1')
  })
})

Cypress Version

14.3.3

Node version

v20.18.1

Operating System

macOS 15.4.1

Debug Logs

Other

No response

@jennifer-shehane
Copy link
Member

I'm surprised this has never been surfaced before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy