0
0
Cypresstesting~10 mins

Task command for Node operations in Cypress - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Cypress task that returns the current working directory.

Cypress
Cypress.Commands.add('getCwd', () => {
  return cy.task([1])
});
Drag options to blanks, or click blank then click option'
A'workingDir'
B'cwd'
C'currentDir'
D'getCwd'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an incorrect task name that is not registered in plugins.
Forgetting to wrap the task name in quotes.
2fill in blank
medium

Complete the code to register a Cypress task that reads a file asynchronously.

Cypress
module.exports = (on, config) => {
  on('task', {
    readFile([1]) {
      const fs = require('fs').promises;
      return fs.readFile(path, 'utf8');
    }
  });
};
Drag options to blanks, or click blank then click option'
Apath
BfilePath
Cfilename
Dfile
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from the variable used inside the function.
Not using async/await or promises for reading files.
3fill in blank
hard

Fix the error in the Cypress task that writes data to a file.

Cypress
on('task', {
  writeFile([1]) {
    const fs = require('fs');
    return fs.writeFileSync(filePath, data);
  }
});
Drag options to blanks, or click blank then click option'
AfilePath
BfilePath, data
C{filePath, data}
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Passing parameters without destructuring causing undefined variables.
Using writeFileSync without proper parameters.
4fill in blank
hard

Fill both blanks to create a Cypress task that deletes a file and returns success status.

Cypress
on('task', {
  deleteFile([1]) {
    const fs = require('fs');
    try {
      fs.unlinkSync([2]);
      return true;
    } catch (err) {
      return false;
    }
  }
});
Drag options to blanks, or click blank then click option'
AfilePath
Bpath
Dfilename
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing runtime errors.
Not handling exceptions properly.
5fill in blank
hard

Fill all three blanks to define a Cypress task that lists files in a directory and filters by extension.

Cypress
on('task', {
  listFiles([1]) {
    const fs = require('fs');
    const files = fs.readdirSync([2]);
    return files.filter(file => file.endsWith([3]));
  }
});
Drag options to blanks, or click blank then click option'
AdirPath
Bdirectory
Dext
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing errors.
Not filtering files correctly by extension.