Cypress for Electron - Using FS

Viewed 2970

I am using Cypress to test my Electron application. Since Cypress uses the browser mode, FS is not supported. So I am getting this error:

Error in mounted hook: "TypeError: fs.existsSync is not a function"

And I found this on the documentation:

https://docs.cypress.io/api/commands/task.html#Event

So I added this on my test:

it('Sample test', () => {
  cy.task('readSettingsJson', settingsFolder).then((content) => { 
    // This can print the JSON file contents correctly
    console.log('content = ' + content) 
  })
})

And on my plugins/index.js:

 on('task', {
    readSettingsJson(foldername) {
      if (!fs.existsSync(foldername)) {
        fs.mkdirSync(foldername, { recursive: true })
        // some command to copy the file
      } else {
        // This is what I am testing at this moment
        return fs.readFileSync(path.join(filename, '/settings.json'), 'utf8')
      }

      return null
    }
 })

However, it doesnt seem to work. I still get the error:

Error in mounted hook: "TypeError: fs.existsSync is not a function"

And despite the test printing the json file correctly, my app still can't load the JSON file.

Am I missing anything? Help please!

1 Answers
Related