0
0
Cypresstesting~20 mins

Task command for Node operations in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node Task Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
});
ALogs the content of 'sample.txt' as a string in the Cypress test runner
BThrows a syntax error because fs.readFileSync is used incorrectly
CFails with an error because 'task' is not defined in Cypress commands
DLogs an object with file metadata instead of content
Attempts:
2 left
💡 Hint
Remember that Cypress tasks run Node code outside the browser context and can return values to the test.
assertion
intermediate
1: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?
});
Aassert.isTrue(count === 5);
Bassert.isFalse(count !== 5);
Cexpect(count).to.equal(5);
Dexpect(count).to.be.greaterThan(5);
Attempts:
2 left
💡 Hint
Use the most direct and readable assertion for equality.
🔧 Debug
advanced
2: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');
});
AThe directory path passed is incorrect or does not exist, so fs.readdirSync throws an error
BThe 'on' function is not properly imported in plugins file
CThe task function is missing a return statement
DThe test code does not handle the promise returned by cy.task
Attempts:
2 left
💡 Hint
Check if the directory path is valid and accessible by Node.
🧠 Conceptual
advanced
1: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?
ABecause task commands automatically retry on failure
BBecause task commands run faster than browser code
CBecause Cypress does not support asynchronous code in tests
DBecause Cypress test code runs in the browser and cannot access Node APIs directly
Attempts:
2 left
💡 Hint
Think about the environment where Cypress test code runs versus where Node code runs.
framework
expert
3: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?
A
// plugins/index.js
module.exports = (on) => {
  const fs = require('fs');
  on('task', {
    deleteFile(filePath) {
      return fs.unlink(filePath);
    }
  });
};

// Test code
cy.task('deleteFile', 'cypress/fixtures/temp.txt').then(() => {
  cy.log('File deleted');
});
B
// plugins/index.js
module.exports = (on) => {
  const fs = require('fs');
  on('task', {
    deleteFile(filePath) {
      fs.unlinkSync(filePath);
      return null;
    }
  });
};

// Test code
cy.task('deleteFile', 'cypress/fixtures/temp.txt').then((result) => {
  expect(result).to.be.null;
});
C
// plugins/index.js
module.exports = (on) => {
  const fs = require('fs');
  on('task', {
    deleteFile(filePath) {
      fs.unlinkSync(filePath);
    }
  });
};

// Test code
cy.task('deleteFile', 'cypress/fixtures/temp.txt').then((res) => {
  expect(res).to.equal('deleted');
});
D
// plugins/index.js
module.exports = (on) => {
  const fs = require('fs');
  on('task', {
    deleteFile(filePath) {
      fs.unlink(filePath, (err) => {
        if (err) throw err;
      });
      return 'done';
    }
  });
};

// Test code
cy.task('deleteFile', 'cypress/fixtures/temp.txt').then((res) => {
  expect(res).to.equal('done');
});
Attempts:
2 left
💡 Hint
Remember that tasks must return a value or null, and asynchronous callbacks must be handled properly.