Cypress: Misleading cy.task error message when returning Promise<undefined>

Created on 24 Jan 2020  路  6Comments  路  Source: cypress-io/cypress

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

first-timers-only pkserver error message

All 6 comments

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

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

Yes, I am curious about this constraint too.

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

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.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

weskor picture weskor  路  3Comments

verheyenkoen picture verheyenkoen  路  3Comments

tahayk picture tahayk  路  3Comments

simonhaenisch picture simonhaenisch  路  3Comments

rbung picture rbung  路  3Comments