node.js - Cypress Electron 产品-使用FS

标签 node.js vue.js electron cypress fs

我正在使用 Cypress 来测试我的Electron应用程序。
由于 Cypress 使用浏览器模式,因此不支持FS
所以我得到这个错误:

Error in mounted hook: "TypeError: fs.existsSync is not a function"
我在文档中发现了这一点:

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


所以我在测试中添加了这个:
it('Sample test', () => {
  cy.task('readSettingsJson', settingsFolder).then((content) => { 
    // This can print the JSON file contents correctly
    console.log('content = ' + content) 
  })
})
在我的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
    }
 })
但是,它似乎不起作用。我仍然收到错误:
Error in mounted hook: "TypeError: fs.existsSync is not a function"
尽管测试可以正确打印json文件,但我的应用仍然无法加载JSON文件。
我有什么想念的吗?请帮忙!

最佳答案

Cypress 对Electron应用程序的支持是在很早的Alpha版本中发布的:
https://www.cypress.io/blog/2019/09/26/testing-electron-js-applications-using-cypress-alpha-release/
或者,尝试使用Spectron,这是Electron团队当前建议的测试框架:
https://www.electronjs.org/spectron

关于node.js - Cypress Electron 产品-使用FS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63111949/

相关文章:

reactjs - Webpack 在 '91% additional asset processing' 期间卡住

node.js - Heroku 的 Rest API 使用插件

javascript - Vue.js - 我该如何解决 [Vue 警告] : Error in render: "TypeError: Cannot read property ' port' of undefined"?

javascript - 我如何能够在 vue js html 中以给定格式多次选择和传递数据?

javascript - 当页面位于底部区域时使用 vue js 加载更多数据

node.js - System.import 异步模块加载与脚本标签同步加载冲突

javascript - Express.js 4 : How to access app. locals.<myvar> 在routes/index.js 中?

javascript - 带有子文档的复杂架构

node.js - 使用 mocha/superagent 测试本地 https 服务器

javascript - Uml/sequence/package/... JavaScript 和 Electron 应用程序的图表替代方案?