cypress cy.task() returned undefined from plugins file
Diagnoses why a Cypress task resolves undefined and how to make it return a value. Use when cy.task().then() receives undefined unexpectedly. Not for tasks that hang or throw.
TL;DR
A task resolves undefined when the plugin function returns nothing, so return a value or a promise from every task handler. Check the plugins file for a missing return, especially in async handlers where the promise was created but never returned.
The query
cypress cy.task() returned undefined from plugins fileUse this when
- cy.task() yields undefined
- a seeding task silently does nothing
- plugin file changes seem to have no effect
Not for
- tasks that throw errors (read the stack trace)
- tasks that time out
- browser-side code issues
Steps
- Open the plugins file and find the task by name in the object passed to on('task', ...). Expected output: the task name exists in the registered task map.
- Read the handler and confirm it returns something: a plain value for sync work, or the promise for async work. Expected output: a return statement on every code path of the handler.
- If the handler is async, make sure the promise chain is returned, not just created inside the function body. Expected output: the keyword return precedes the async call or the awaited result.
- Restart the Cypress runner so the plugins file reloads, then call the task from a spec and log the result. Expected output: the logged value matches what the handler returned.
- Add a small assertion on the task result in the spec so regressions surface immediately. Expected output: the spec fails loudly if the task ever returns undefined again.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_ZTRRFyRz9yUSoXA6W4EbfQ
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.