Challenge - 5 Problems
Node Task Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Cypress task command?
Consider this Cypress task command that reads a file using Node.js fs module. What will be logged in the test runner console?
Cypress
Cypress.Commands.add('readFileTask', (filePath) => { return cy.task('readFile', filePath); }); // In plugins/index.js module.exports = (on) => { const fs = require('fs'); on('task', { readFile(filePath) { return fs.readFileSync(filePath, 'utf8'); } }); }; // Test code cy.readFileTask('cypress/fixtures/sample.txt').then((content) => { cy.log(content); });
Attempts:
2 left
💡 Hint
Remember that Cypress tasks run Node code outside the browser context and can return values to the test.
✗ Incorrect
The task 'readFile' uses Node's fs.readFileSync to read the file content synchronously and returns it as a string. The Cypress command calls this task and logs the string content.
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies the result of a Cypress task?
You have a Cypress task that returns the number of files in a directory. Which assertion correctly checks that the count is 5?
Cypress
cy.task('countFiles', 'cypress/fixtures').then((count) => { // Which assertion goes here? });
Attempts:
2 left
💡 Hint
Use the most direct and readable assertion for equality.
✗ Incorrect
The assertion expect(count).to.equal(5) directly checks that count equals 5, which is clear and idiomatic in Cypress tests.
🔧 Debug
advanced2:30remaining
Why does this Cypress task fail with 'Cannot read property' error?
Given this task code, why does the test fail with "ENOENT: no such file or directory" error?
Cypress
// plugins/index.js module.exports = (on) => { on('task', { countFiles(dir) { const fs = require('fs'); const files = fs.readdirSync(dir); return files.length; } }); }; // Test code cy.task('countFiles', 'cypress/fixtures').then((count) => { expect(count).to.be.a('number'); });
Attempts:
2 left
💡 Hint
Check if the directory path is valid and accessible by Node.
✗ Incorrect
If the directory path is wrong, fs.readdirSync throws an error (e.g., ENOENT), causing the task to fail.
🧠 Conceptual
advanced1:30remaining
What is the main benefit of using Cypress task commands for Node operations?
Why should you use Cypress task commands to perform Node.js operations instead of running them directly in test code?
Attempts:
2 left
💡 Hint
Think about the environment where Cypress test code runs versus where Node code runs.
✗ Incorrect
Cypress test code runs inside the browser environment, which does not have access to Node.js APIs like file system or OS modules. Tasks run in the Node environment, enabling such operations.
❓ framework
expert3:00remaining
Which is the correct way to define and use a Cypress task that deletes a file?
You want to create a Cypress task named 'deleteFile' that deletes a file at a given path. Which option correctly defines the task and uses it in a test?
Attempts:
2 left
💡 Hint
Remember that tasks must return a value or null, and asynchronous callbacks must be handled properly.
✗ Incorrect
Option B uses fs.unlinkSync to synchronously delete the file and returns null to indicate success. This matches Cypress task expectations. Option B returns fs.unlink which is async and returns undefined, causing Cypress to hang. Option B does not return anything, so the test assertion fails. Option B returns 'done' immediately but the async unlink callback may not have completed, causing unreliable behavior.