【问题标题】:Runtime Error occurs when require('electron') with Jest test file当 require('electron') 与 Jest 测试文件时发生运行时错误
【发布时间】:2016-02-19 18:04:12
【问题描述】:

当我import 'electron' 并运行 Jest 时,出现以下错误。使用requireimport 没关系。 jest.autoMockOff() 也无济于事。

 FAIL  __tests__/root_test.js
● Runtime Error
TypeError: Cannot read property 'on' of undefined

版本如下。

  • 节点:v4.2.1
  • jest-cli: v0.8.2
  • 电子预建:v0.36.4

谁能帮帮我?

【问题讨论】:

  • 你是如何运行 JestJS 的?
  • 我将 "scripts": {"test": "jest"} 写入 package.json 并使用 npm test 运行。而我的 npm 版本是 2.14.7
  • 作为一种解决方法,我将 const remote = require('electron').remote; 从文件顶部移到函数中,并且测试通过了。

标签: node.js electron jestjs


【解决方案1】:

如果您的测试加载内置 Electron 模块,您必须在 Electron 进程中运行它们,内置 Electron 模块在常规 Node 进程中不可用(如果您只是从命令行)。

【讨论】:

  • 哦,谢谢,这很有道理。但实际上,我在 React 组件中使用 remote 模块并想对其进行测试。有什么办法吗?
  • 我认为 JestJS 没有任何现成的解决方案,但如果 Jest 可以用作库,您可以自行开发。如果您使用的是 Mocha,我会说查看 github.com/jprichardson/electron-mochagithub.com/segmentio/nightmare(取决于您要测试的内容)。
【解决方案2】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-18
    • 2018-09-11
    • 2018-05-29
    • 2015-07-31
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多