mirror of
https://github.com/cypress-io/cypress.git
synced 2026-04-28 19:00:03 -05:00
fix: cy.task() can pass undefined as default argument. (#8600)
Co-authored-by: Gleb Bahmutov <gleb.bahmutov@gmail.com>
This commit is contained in:
@@ -38,6 +38,10 @@ describe('src/cy/commands/task', () => {
|
||||
cy.task('return:arg', 'works').should('eq', 'works')
|
||||
})
|
||||
|
||||
it('returns the default value when no argument is given', () => {
|
||||
cy.task('arg:is:undefined').should('eq', 'arg was undefined')
|
||||
})
|
||||
|
||||
describe('.log', () => {
|
||||
beforeEach(function () {
|
||||
this.logs = []
|
||||
@@ -205,7 +209,7 @@ describe('src/cy/commands/task', () => {
|
||||
expect(lastLog.get('error')).to.eq(err)
|
||||
expect(lastLog.get('state')).to.eq('failed')
|
||||
|
||||
expect(err.message).to.eq(`\`cy.task('bar')\` failed with the following error:\n\nThe task 'bar' was not handled in the plugins file. The following tasks are registered: return:arg, wait, create:long:file\n\nFix this in your plugins file here:\n${Cypress.config('pluginsFile')}\n\nhttps://on.cypress.io/api/task`)
|
||||
expect(err.message).to.eq(`\`cy.task('bar')\` failed with the following error:\n\nThe task 'bar' was not handled in the plugins file. The following tasks are registered: return:arg, arg:is:undefined, wait, create:long:file\n\nFix this in your plugins file here:\n${Cypress.config('pluginsFile')}\n\nhttps://on.cypress.io/api/task`)
|
||||
|
||||
done()
|
||||
})
|
||||
|
||||
@@ -38,6 +38,13 @@ module.exports = (on) => {
|
||||
'return:arg' (arg) {
|
||||
return arg
|
||||
},
|
||||
'arg:is:undefined' (arg) {
|
||||
if (arg === undefined) {
|
||||
return 'arg was undefined'
|
||||
}
|
||||
|
||||
throw new Error(`Expected arg to be undefined, but it was ${arg}`)
|
||||
},
|
||||
'wait' () {
|
||||
return Promise.delay(2000)
|
||||
},
|
||||
|
||||
@@ -32,7 +32,13 @@ const merge = (prevEvents, events) => {
|
||||
|
||||
const wrap = (ipc, events, ids, args) => {
|
||||
const task = args[0]
|
||||
const arg = args[1]
|
||||
let arg = args[1]
|
||||
|
||||
// ipc converts undefined to null.
|
||||
// we're restoring it.
|
||||
if (arg && arg.__cypress_task_no_argument__) {
|
||||
arg = undefined
|
||||
}
|
||||
|
||||
const invoke = (eventId, args = []) => {
|
||||
const handler = _.get(events, `${eventId}.handler.${task}`)
|
||||
|
||||
@@ -119,6 +119,14 @@ const init = (config, options) => {
|
||||
invocationId,
|
||||
}
|
||||
|
||||
// no argument is passed for cy.task()
|
||||
// This is necessary because undefined becomes null when it is sent through ipc.
|
||||
if (args[1] === undefined) {
|
||||
args[1] = {
|
||||
__cypress_task_no_argument__: true,
|
||||
}
|
||||
}
|
||||
|
||||
ipc.send('execute', registration.event, ids, args)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user