Test Overview
This test uses Cypress task command to perform a Node.js operation that reads a file's content. It verifies that the content returned by the task matches the expected text.
This test uses Cypress task command to perform a Node.js operation that reads a file's content. It verifies that the content returned by the task matches the expected text.
/// <reference types="cypress" /> // In cypress/plugins/index.js or cypress.config.js module.exports = (on, config) => { on('task', { readFileContent(filename) { const fs = require('fs') return fs.readFileSync(filename, 'utf8') } }) } // In the test file describe('Task command for Node operations', () => { it('reads file content using task command', () => { const testFile = 'cypress/fixtures/sample.txt' cy.task('readFileContent', testFile).then((content) => { expect(content).to.contain('Hello Cypress') }) }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Cypress test runner initialized | — | PASS |
| 2 | Cypress opens browser and loads test page | Browser opened with Cypress test environment | — | PASS |
| 3 | Test calls cy.task('readFileContent', 'cypress/fixtures/sample.txt') | Cypress sends task request to Node process | — | PASS |
| 4 | Node process executes task: reads file content synchronously | File 'sample.txt' content read: 'Hello Cypress! This is a sample file.' | — | PASS |
| 5 | Task returns file content to Cypress test | Cypress receives file content string | — | PASS |
| 6 | Test asserts content includes 'Hello Cypress' | Content string contains expected text | expect(content).to.contain('Hello Cypress') | PASS |
| 7 | Test ends successfully | Test passed, no errors | — | PASS |
cy.task() command in Cypress?cy.task()cy.task() command allows Cypress tests to execute Node.js code outside the browser environment.cy.task() runs backend code for tasks like file access.cy.task() runs backend Node code [OK]cy.task() with DOM commandscy.task() runs in the browsercy.task() is for assertionsreadFile in a Cypress test?cy.task('taskName').cy.readFile is a different command for reading files in the browser context, not tasks. The others are invalid commands.cy.task() to call tasks [OK]cy.task('name') to run tasks [OK]cy.readFile instead of cy.taskcy.nodeTaskcypress.config.js:
tasks: {
getEnv() {
return process.env.NODE_ENV
}
}
What will this test print?
cy.task('getEnv').then(env => {
cy.log(env)
})getEnv returns the value of process.env.NODE_ENV, which is the environment mode (like 'development' or 'production').cy.task('getEnv') and logs the returned value, so it will print the current environment string.cy.task() logs them [OK]cypress.config.js?
tasks: {
readFile(path) {
const fs = require('fs')
return fs.readFileSync(path, 'utf8')
}
}
When called as cy.task('readFile', 'data.txt'), it throws an error.readFileSync is allowed but can cause blocking issues.fs.promises.readFile.require is disallowed inside tasksfs.promises.readFile with await to read the file asynchronously, then parses JSON.Object.keys() of the parsed JSON, which gives the top-level keys as required..keys() on the parsed object (should use Object.keys()). Options C and D try to get keys from raw string data, which is incorrect.Pass the test and this pattern moves unknown