twolfson / karma-electron

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

Can't import nodejs modules in an angular-cli project (Typescript) #34

Closed hoefling closed 5 years ago

hoefling commented 5 years ago

I guess this is an issue similar to #27, however I double-checked the Karma configuration. I have prepared a sample repository reproducing the issue:

$ git clone https://github.com/hoefling/karma-electron-showcase
...
$ yarn install
...
$ yarn test
yarn run v1.10.1
$ npm run postinstall:electron && ng test

> angular-electron@4.2.2 postinstall:electron /Users/hoefling/projects/private/karma-electron-showcase
> node postinstall

 10% building modules 1/1 modules 0 active
START:
(node:15743) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
15 10 2018 17:21:34.344:INFO [karma]: Karma v3.0.0 server started at http://0.0.0.0:9876/
15 10 2018 17:21:34.345:INFO [launcher]: Launching browser Electron with unlimited concurrency
15 10 2018 17:21:34.350:INFO [launcher]: Starting browser Electron

Finished in 0 secs / 0 secs @ 17:21:37 GMT+0200 (CEST)

✖ 「wdm」: Hash: 228257890eb55d0baf07
Version: webpack 4.8.3
Time: 6346ms
Built at: 15.10.2018 17:21:37
         Asset      Size     Chunks  Chunk Names
background.jpg   227 KiB
       main.js  34.5 KiB       main  main
  polyfills.js   514 KiB  polyfills  polyfills
     vendor.js  7.12 MiB     vendor  vendor
Entrypoint main = vendor.js main.js
Entrypoint polyfills = polyfills.js
[./node_modules/@angular/compiler/fesm5/testing.js] 11.4 KiB {vendor} [built]
[./node_modules/@angular/core/fesm5/core.js] 723 KiB {vendor} [built]
[./node_modules/@angular/core/fesm5/testing.js] 47.9 KiB {vendor} [built]
[./node_modules/@angular/platform-browser-dynamic/fesm5/platform-browser-dynamic.js] 20.2 KiB {vendor} [built]
[./node_modules/@angular/platform-browser-dynamic/fesm5/testing.js] 13.3 KiB {vendor} [built]
[./node_modules/@angular/platform-browser/fesm5/testing.js] 6.24 KiB {vendor} [built]
[./node_modules/core-js/es7/reflect.js] 510 bytes {polyfills} [built]
[./node_modules/zone.js/dist/zone-testing.js] 67.4 KiB {vendor} [built]
[./node_modules/zone.js/dist/zone.js] 127 KiB {polyfills} [built]
[./src sync recursive \.spec\.ts$] ./src sync \.spec\.ts$ 250 bytes {main} [built]
[./src/app/components/home/home.component.spec.ts] 1.09 KiB {main} [optional] [built]
[./src/app/services/dummy.service.spec.ts] 690 bytes {main} [optional] [built]
[./src/polyfills-test.ts] 139 bytes {polyfills} [built]
[./src/test.ts] 656 bytes {main} [built]
[0] multi ./src/polyfills-test.ts 28 bytes {polyfills} [built]
    + 290 hidden modules

ERROR in ./src/app/services/dummy.service.ts
Module not found: Error: Can't resolve 'fs' in '/Users/hoefling/projects/private/karma-electron-showcase/src/app/services'
 @ ./src/app/services/dummy.service.ts 13:9-22
 @ ./src/app/services/dummy.service.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

So basically, when I do

import * as fs from 'fs';

class Foo {
  bar() {
    fs.readdirSync('/tmp');
  }
}

the module is not resolved in tests. The current workaround I'm using is using window.require:

class Foo {
  bar() {
    window.require('fs').readdirSync('/tmp');
  }
}

What should I change in karma.conf.js in the above example in order to be able to import fs as usual?

twolfson commented 5 years ago

karma runs in the browser context of Electron, not the main process section. While require works fine, fs needs a hoop or 2 to jump through. I believe require('electron').remote.require('fs') should work

Another way to handle this is by adding browserify with a brfs transform to your transform step. This will inline sync commands like readdirSync and readFileSync to show their results. Though this might interfere with require usage by forcing bundling =/