Test Overview
This test checks a custom Cypress plugin that logs a message before the test run. It verifies the plugin runs and the log appears in the console.
This test checks a custom Cypress plugin that logs a message before the test run. It verifies the plugin runs and the log appears in the console.
/// <reference types="cypress" /> // cypress/plugins/index.js module.exports = (on, config) => { on('before:run', () => { console.log('Custom plugin: Test run is starting'); }); }; // cypress/e2e/sample_spec.cy.js describe('Custom Plugin Test', () => { it('should run a simple test and trigger plugin log', () => { cy.visit('https://example.cypress.io'); cy.contains('type').click(); cy.url().should('include', '/commands/actions'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Cypress test runner starts and loads plugins | Cypress initializes and loads cypress/plugins/index.js | — | PASS |
| 2 | Custom plugin logs message before test run | Console shows 'Custom plugin: Test run is starting' | Verify console log message appears | PASS |
| 3 | Test visits https://example.cypress.io | Browser opens example Cypress page | — | PASS |
| 4 | Test clicks on link containing text 'type' | Page navigates to /commands/actions | URL includes '/commands/actions' | PASS |
| 5 | Assertion checks URL contains '/commands/actions' | URL is https://example.cypress.io/commands/actions | cy.url().should('include', '/commands/actions') | PASS |
cypress/plugins/index.js file where Node.js code runs.support/commands.js is for custom commands in browser tests, integration/spec.js is for test specs, and cypress.json is config.cypress/plugins/index.js:
module.exports = (on, config) => {
on('task', {
logMessage(message) {
console.log(message)
return null
}
})
}
What happens when the test calls cy.task('logMessage', 'Hello')?cy.task('logMessage', 'Hello') sends 'Hello' to the plugin task, which logs it in Node.js, not browser, and returns null.module.exports = (on, config) => {
on('task', {
multiplyNumbers(a, b) {
return a * b
}
})
}
And select the correct fix.fs can be used to read files synchronously or asynchronously.cy.readFile is a test command, not available in plugins. Browser fetch is not available in Node.js plugin context. Using fs.readFileSync and returning parsed JSON is correct.Pass the test and this pattern moves unknown