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.
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.).
// plugins/index.js
module.exports = (on, config) => {
on("task", {
exmapleAsyncTask() {
return new Promise(resolve => setTimeout(resolve, 100));
}
});
};
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));
}
Cypress 3.8.2
macOS 10.14.6
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.