Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module paths are messed up #41

Closed
jushar opened this issue Jun 12, 2019 · 5 comments
Closed

Module paths are messed up #41

jushar opened this issue Jun 12, 2019 · 5 comments
Labels

Comments

@jushar
Copy link

jushar commented Jun 12, 2019

Description
If the nodeIntegration is enabled via the workaround mentioned in #38 (comment), 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
Copy link
Owner

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
Copy link
Author

jushar commented Jun 19, 2019

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
Copy link
Owner

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
Copy link
Author

jushar commented Jun 21, 2019

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
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants