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

Open
GrzegorzKazana opened this issue Jan 24, 2020 · 4 comments
Open

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

GrzegorzKazana opened this issue Jan 24, 2020 · 4 comments

Comments

@GrzegorzKazana
Copy link

@GrzegorzKazana GrzegorzKazana commented Jan 24, 2020

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

This comment has been minimized.

Copy link
Member

@jennifer-shehane 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

@vicrac

This comment has been minimized.

Copy link

@vicrac 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

This comment has been minimized.

Copy link

@brettz9 brettz9 commented Feb 17, 2020

Yes, I am curious about this constraint too.

@brettz9

This comment has been minimized.

Copy link

@brettz9 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

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

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.