Bird
0
0

You want to create a custom plugin that reads a JSON file from disk and returns its content to your test. Which approach correctly implements this plugin task?

hard📝 Application Q15 of 15
Cypress - Plugins and Ecosystem
You want to create a custom plugin that reads a JSON file from disk and returns its content to your test. Which approach correctly implements this plugin task?
AUse <code>console.log</code> to print file content and return undefined
BUse <code>cy.readFile</code> inside the plugin task to read the file asynchronously
CUse browser <code>fetch</code> API inside the plugin task to get the file content
DUse Node.js <code>fs.readFileSync</code> inside the plugin task and return parsed JSON
Step-by-Step Solution
Solution:
  1. Step 1: Understand plugin environment

    Plugins run in Node.js, so Node.js modules like fs can be used to read files synchronously or asynchronously.
  2. Step 2: Choose correct method to read file

    cy.readFile is a test command, not available in plugins. Browser fetch is not available in Node.js plugin context. Using fs.readFileSync and returning parsed JSON is correct.
  3. Final Answer:

    Use Node.js fs.readFileSync inside the plugin task and return parsed JSON -> Option D
  4. Quick Check:

    Plugins use Node.js fs to read files = A [OK]
Quick Trick: Use Node.js fs module in plugins, not cy commands [OK]
Common Mistakes:
  • Trying to use cy commands inside plugins
  • Using browser APIs in Node.js plugin code
  • Returning undefined instead of file content

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes