twolfson / karma-electron

Karma launcher and preprocessor for Electron
The Unlicense
59 stars 21 forks source link

Module paths are messed up #41

Closed jushar closed 5 years ago

jushar commented 5 years ago

Description If the nodeIntegration is enabled via the workaround mentioned in https://github.com/twolfson/karma-electron/issues/38#issuecomment-491510277, it seems to mess up the module paths which leads to modules not being able to be loaded from node_modules. Precisely, outputting the value of module.paths in the Electron window (via devtools) gives me:

> module.paths
(2) ["C:\Dev\karma-electron-repro\testa…tron\dist\resources\default_app.asar\node_modules", "C:\Dev\karma-electron-repro\testa…node_modules\electron\dist\resources\node_modules"]
0: "C:\Dev\karma-electron-repro\testapp\node_modules\electron\dist\resources\default_app.asar\node_modules"
1: "C:\Dev\karma-electron-repro\testapp\node_modules\electron\dist\resources\node_modules"
length: 2
__proto__: Array(0)

This does not comply with the behaviour of a "directly launched" Electron which also adds the node_modules folder of the directory it is launched from to the module search path.

Essentially, this behaviour leads to modules that are placed in node_modules not being found.

Reproduction

  1. Clone https://github.com/Jusonex/karma-electron-testapp
  2. npm install
  3. Apply require hackfix by adding a require line with the absolute path to the nodeIntegration enable script after https://github.com/twolfson/karma-electron/blob/master/lib/electron-launcher.js#L40-L42
  4. npm test
  5. Open developer tools in the Electron window.
  6. Type require('lodash')
  7. Type module.paths
twolfson commented 5 years ago

This is likely caused by the same issue in #40. We need to wrap all files to provide the proper paths for require and module.paths to load against

https://github.com/twolfson/karma-electron/blob/6.1.1/lib/node-integration-iframe.mustache.js#L36

Please see README for setting up preprocessors and client.useIframe

https://github.com/twolfson/karma-electron/tree/6.1.1#getting-started

jushar commented 5 years ago

First of all, thanks for your great work on issue #40.

I migrated the test project to version 6.3.0 and added preprocessors and client.useIframe as you suggested. However, I'm still experiencing the same problem.

You can review all my changes to a default Angular installation here: https://github.com/Jusonex/karma-electron-testapp/compare/ddbab1e...master

I also added a require("sqlite3") line that demonstrates the problem in a more practical manner. Since sqlite3 contains native modules, it has to be loaded as webpack external. You can reproduce the problem by just running npm test and opening the dev tools.

twolfson commented 5 years ago

Okay, let's start from the beginning to I fully understand the issue

What is the behavior you're seeing on launch? Are tests running? What's the expected behavior?

jushar commented 5 years ago

What is the behavior you're seeing on launch?

21 06 2019 10:01:44.822:INFO [karma-server]: Karma v4.0.1 server started at http://0.0.0.0:9876/
21 06 2019 10:01:44.823:INFO [launcher]: Launching browsers CustomElectron with concurrency unlimited
karma-electron: `--show` is now deprecated. Please use `browserWindowOptions.show` instead
21 06 2019 10:01:44.832:INFO [launcher]: Starting browser Electron
21 06 2019 10:01:49.666:WARN [karma]: No captured browser, open http://localhost:9876/
21 06 2019 10:01:49.985:INFO [Electron 5.0.4 (Node 12.0.0)]: Connected on socket CdMl_yYqQ550D6QAAAAA with id 18121251
Electron 5.0.4 (Node 12.0.0) ERROR
  {
    "message": "Uncaught Error: Cannot find module 'sqlite3'\nRequire stack:\n- C:\\Dev\\karma-electron-repro\\testapp\\node_modules\\electron\\dist\\resources\\electron.asar\\renderer\\init.js\nat internal/modules/cjs/loader.js:662:5\n\nundefined",
    "str": "Uncaught Error: Cannot find module 'sqlite3'\nRequire stack:\n- C:\\Dev\\karma-electron-repro\\testapp\\node_modules\\electron\\dist\\resources\\electron.asar\\renderer\\init.js\nat internal/modules/cjs/loader.js:662:5\n\nundefined"
  }

Are tests running?

No, because it errors before tests are executed.

What's the expected behavior?

I expect the error message to not appear and tests run successfully. In ng serve/webpack-dev-server mode, using modules from the node_modules folder works perfectly fine (assuming webpack externals are set up correctly, but that's unrelated to this issue).

twolfson commented 5 years ago

Ah, okay. It sounds like that could be us but it could also be a configuration error or a missing plugin

The repo has too much content for me to quickly and easily verify that the issue is karma-electron. Can you create a minimal version which only has karma-electron, your testing framework (e.g. jasmine), and is reproducing the error?