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

Misleading cy.task error message when returning Promise<undefined> #6241

Closed
GrzegorzKazana opened this issue Jan 24, 2020 · 6 comments · Fixed by #6920
Closed

Misleading cy.task error message when returning Promise<undefined> #6241

GrzegorzKazana opened this issue Jan 24, 2020 · 6 comments · Fixed by #6920
Labels
good first issue Good for newcomers pkg/server This is due to an issue in the packages/server directory type: error message

Comments

@GrzegorzKazana
Copy link

Current behavior:

When running a task returning promise that fulfils with undefined, we get an error with following message: The task 'exampleAsyncTask' returned undefined. You must return a promise, a value, or null to indicate that the task was handled.

Desired behavior:

Cypress should either handle promises fulfilling to undefined, or give clear error message that it's not allowed (i.e. The task 'exampleAsyncTask' returned promise fulfilling with undefined.).

Test code to reproduce

  1. Clone https://github.com/cypress-io/cypress-test-tiny
  2. Create simple task that returns promise resolving with undefined
// plugins/index.js
module.exports = (on, config) => {
  on("task", {
    exmapleAsyncTask() {
      return new Promise(resolve => setTimeout(resolve, 100));
    }
  });
};
  1. Execute the task in test
describe("page", () => {
  it("works", () => {
    cy.visit("https://example.cypress.io");
    cy.task("exmapleAsyncTask");
  });
});

Test fails with:

CypressError: cy.task('exmapleAsyncTask') failed with the following error:

The task 'exmapleAsyncTask' returned undefined. You must return a promise, a value, or null to indicate that the task was handled.

Changing task promise to return any value fixes the issue:

exmapleAsyncTask() {
      return new Promise(resolve => setTimeout(() => resolve(42), 100));
}

Versions

Cypress 3.8.2
macOS 10.14.6

@jennifer-shehane
Copy link
Member

jennifer-shehane commented Jan 28, 2020

We can update the error message to be more descriptive that the Promise cannot resolve to undefined.

The task 'foo' returned undefined. You must return a value, null, or a Promise that
resolves to a value or null to indicate that the task was handled.

This error message can be updated here:

https://github.com/cypress-io/cypress/blob/develop/packages/server/lib/task.coffee#L36:L36

@jennifer-shehane jennifer-shehane added the pkg/server This is due to an issue in the packages/server directory label Jan 28, 2020
@cypress-bot cypress-bot bot added the stage: ready for work The issue is reproducible and in scope label Jan 28, 2020
@jennifer-shehane jennifer-shehane added type: error message stage: ready for work The issue is reproducible and in scope good first issue Good for newcomers and removed stage: ready for work The issue is reproducible and in scope labels Jan 28, 2020
@vicrac
Copy link

vicrac commented Jan 28, 2020

To be honest, I wonder why is there such a constraint? Is undefined (or Promise<undefined>) returned from task used to indicate that it failed (or somehow else processed by Cypress)?

@jennifer-shehane

@brettz9
Copy link

brettz9 commented Feb 17, 2020

Yes, I am curious about this constraint too.

@brettz9
Copy link

brettz9 commented Feb 17, 2020

My apologies, I see the docs explain it thus:

In the task plugin event, the command will fail if undefined is returned. This helps catch typos or cases where the task event is not handled.

Source: https://docs.cypress.io/api/commands/task.html#Command

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 6, 2020

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

@cypress-bot cypress-bot bot removed the stage: needs review The PR code is done & tested, needs review label Apr 6, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 13, 2020

Released in 4.4.0.

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

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Apr 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers pkg/server This is due to an issue in the packages/server directory type: error message
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants