geblanco / electron-router

Router solving Electron hell callback passing, helpfull for MVC
MIT License
123 stars 6 forks source link

jest testing - router cannot be created #3

Closed utillity closed 6 years ago

utillity commented 6 years ago

first of all thanks for the great module. I am using it in a multi-threaded (multi-window, but only the main window is visible) application which gathers information from various channels (IP, CAN-bus, etc.) and displays it in the UI.

When I test an object (using jest) which creates a router, Router(name) fails with the following error:

  at Router._isRenderProcess (node_modules/electron-router/router.js:153:20)
  at Router._setup (node_modules/electron-router/router.js:117:18)
  at Router (node_modules/electron-router/router.js:110:12)
  at node_modules/electron-router/router.js:440:17
  at new ChangeHandler (src/common/changeHandler.ts:22:20)
  at Object.it (__tests__/changeHandler.test.js:21:20)

it seems you access the remote object, which is not available in the JEST environment (since electron is not running). is there a way around this?

geblanco commented 6 years ago

Hi @utillity,

Thank you for your feedback, I'm glad to hear this module is useful to the community.

This module is intended to be run inside an electron environment and is really tied to it. There is not a clear way in which you can test it independently from electron, although it should be possible theoretically. In my opinion, there are various possibilities, I'll try to summarize them:

I expect to this to be useful! :) Please let me know which solution you take at the end or, even better, post it here so others can benefit from it.

Best, Guillermo.

utillity commented 6 years ago

Hi, thanks for the feedback. Well, in my opinion the code could be made a little more robust to just detect that objects it requires are not initialized/available and it could throw an appropriate exception, or just throw errors when trying to send something. Thanks anyways. Tilli

geblanco commented 6 years ago

Yes, I totally agree that the code should handle that, and it does. The first part of the module does exactly that. To be more specific, if you are not running inside electron, the call to require('electron') should fail and the module throws an error for it, maybe jest is patching the require call?

utillity commented 6 years ago

Yes I saw that in your code. I don't know what that remote object is that fails, but there is no check

LG, Tilli


From: m0n0l0c0 notifications@github.com Sent: Friday, January 5, 2018 11:21:55 AM To: m0n0l0c0/electron-router Cc: Tilfried Weissenberger; Mention Subject: Re: [m0n0l0c0/electron-router] jest testing - router cannot be created (#3)

Yes, I totally agree that the code should handle that, and it does. The first parthttps://github.com/m0n0l0c0/electron-router/blob/master/router.js#L16 of the module does exactly that. To be more specific, if you are not running inside electron, the call to require('electron') should fail and the module throws an error for it, maybe jest is patching the require call?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/m0n0l0c0/electron-router/issues/3#issuecomment-355520921, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AFSu9mqmXDjxP5xPaFeybeishX9WnZqyks5tHffDgaJpZM4RTYuX.