/// <reference types="cypress" />
// cypress/plugins/index.js
module.exports = (on, config) => {
on('task', {
logMessage(message) {
console.log(message)
return null
}
})
}
// cypress/e2e/plugin_test.cy.js
describe('Custom plugin task test', () => {
it('should log message via custom plugin task', () => {
cy.task('logMessage', 'Hello from plugin')
})
})The cypress/plugins/index.js file defines a custom task named logMessage. This task receives a message and logs it to the Node.js console. Returning null indicates no value is returned to the test.
The test file plugin_test.cy.js calls cy.task('logMessage', 'Hello from plugin') to invoke the plugin task. When the test runs, the message appears in the terminal console where Cypress runs.
This setup uses Cypress's plugin system correctly, separating plugin code from test code, and uses the task API to communicate between the browser and Node environment.