Sometimes, you need to run Node.js code inside your Cypress tests. The task command lets you do that safely.
You want to read or write files during a test.
You need to run a script or command that only Node.js can do.
You want to get data from the system or environment variables.
You want to perform setup or cleanup tasks outside the browser.
You want to reuse existing Node.js code inside your test.
Syntax
Cypress
In cypress.config.js or plugins file:
on('task', {
taskName(args) {
// Node.js code herereturn result;
}
});
In test file:
cy.task('taskName', args).then((result) => {
// use result in test
});
The task runs in the Node.js environment, not the browser.
Always return a value or a promise from the task function.
Examples
This example logs a message to the Node.js console during the test.
When called as cy.task('readFile', 'data.txt'), it throws an error.
medium
A. The task function must return a Promise or value, but readFileSync is synchronous
B. The task should be defined as an async function returning a Promise
C. Tasks cannot accept arguments
D. The require statement is not allowed inside tasks
Solution
Step 1: Understand task function requirements
Cypress tasks should return a value or a Promise. Using synchronous readFileSync is allowed but can cause blocking issues.
Step 2: Identify best practice for async operations
To avoid errors and improve performance, tasks reading files should be async and return a Promise, e.g., using fs.promises.readFile.
Step 3: Analyze the error cause
The error likely occurs because Cypress expects a Promise for async tasks, so defining the task as async fixes it.
Final Answer:
The task should be defined as an async function returning a Promise -> Option B
Quick Check:
Async tasks return Promises = D [OK]
Hint: Use async functions for tasks doing file I/O [OK]
Common Mistakes:
Thinking synchronous code always works in tasks
Believing require is disallowed inside tasks
Assuming tasks cannot take arguments
5. You want to create a Cypress task that reads a JSON file and returns only the keys of the top-level object. Which task definition is correct?
hard
A. async readKeys(file) {
const fs = require('fs').promises
const data = await fs.readFile(file, 'utf8')
return Object.keys(JSON.parse(data))
}
B. readKeys(file) {
const fs = require('fs')
const data = fs.readFileSync(file, 'utf8')
return JSON.parse(data).keys()
}
C. async readKeys(file) {
const fs = require('fs')
const data = await fs.readFile(file, 'utf8')
return Object.keys(data)
}
D. readKeys(file) {
const fs = require('fs')
const data = fs.readFileSync(file, 'utf8')
return Object.keys(data)
}
Solution
Step 1: Read file asynchronously and parse JSON
async readKeys(file) {
const fs = require('fs').promises
const data = await fs.readFile(file, 'utf8')
return Object.keys(JSON.parse(data))
} correctly uses fs.promises.readFile with await to read the file asynchronously, then parses JSON.
Step 2: Extract keys from parsed object
It returns Object.keys() of the parsed JSON, which gives the top-level keys as required.
Step 3: Identify errors in other options
readKeys(file) {
const fs = require('fs')
const data = fs.readFileSync(file, 'utf8')
return JSON.parse(data).keys()
} incorrectly calls .keys() on the parsed object (should use Object.keys()). Options C and D try to get keys from raw string data, which is incorrect.
Final Answer:
async readKeys(file) {
const fs = require('fs').promises
const data = await fs.readFile(file, 'utf8')
return Object.keys(JSON.parse(data))
} -> Option A
Quick Check:
Async read + JSON parse + Object.keys = A [OK]
Hint: Parse JSON first, then get keys with Object.keys() [OK]
Common Mistakes:
Calling .keys() on object instead of Object.keys()
Trying to get keys from raw string data
Using synchronous read without async/await properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.