我正在使用 Cypress 来测试我的Electron应用程序。
由于 Cypress 使用浏览器模式,因此不支持FS
。
所以我得到这个错误:
Error in mounted hook: "TypeError: fs.existsSync is not a function"
我在文档中发现了这一点:所以我在测试中添加了这个:
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/