Runtime Error occurs when require('electron') with Jest test file - node.js

When I import 'electron' and run Jest, following error occurs. Using require or import don't matter.
And jest.autoMockOff() doesn't help also.
FAIL __tests__/root_test.js
● Runtime Error
TypeError: Cannot read property 'on' of undefined
Versions are as follows.
node: v4.2.1
jest-cli: v0.8.2
electron-prebuilt: v0.36.4
Can anyone help me?

If your tests load built-in Electron modules you have to run them within an Electron process, built-in Electron modules are not available in a regular Node process (which is what you get if you just run jest from the commandline).

Try setting modulePathIgnorePatterns in package.json
https://facebook.github.io/jest/docs/api.html#config-modulepathignorepatterns-array-string

Related

Cypress - Error: Can't resolve 'async_hooks'

Current behavior
I know this kind of issue was fixed in the version 5.1.0 but ...
I'm trying to seed DB in the Node JS part logic at the before hook
We have the cls-hooked and 1 more internal npm module for sharing both use async_hooks
When I'm trying to import and use any of that modules I receive crashes at the test script launch:
Error: Webpack Compilation Error
./node_modules/...[our private module name].../lib/...[file name]....js
Module not found: Error: Can't resolve 'async_hooks' in '/Users/.../node_modules/...[private module or cls-hooked].../lib'
resolve 'async_hooks' in '/Users/.../node_modules/...[private module or cls-hooked].../lib'
Parsed request is a module
using description file: /Users/.../node_modules/...[private module or cls-hooked].../package.json (relative path: ./lib)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
Desired behavior
Test script launch should go smoothly with before hook executing
Test code to reproduce
require some module in ./cypress/plugins/index.ts with next similar logic
var async_hooks = require("async_hooks");
var asyncLocalStorage = new async_hooks.AsyncLocalStorage();
...
asyncLocalStorage.getStore();
Cypress Version
^8.1.0 - 8.5.0
Cypress binary version: 8.5.0
Electron version: 13.2.0
Bundled Node version: 14.16.0
tried Node version: 14.17.6 and 16.9.1
the same for:
Cypress binary version: 7.7.0
Electron version: 12.0.0-beta.14
Bundled Node version: 14.15.1
Other
additional install of "async_hooks": "^1.0.0" didn't help
Using TS
"typescript": "^4.1.5"
OS
macOS Big Sur 11.16
The problem was in 1 common file for both processes browser and NodeJs in Cypress.
In this case, Webpack built the bundle with 'browser' context but received NodeJs context logic. That's why the error occured.
When I used enum in Browser part in the file which besides that also exports NodeJs logic it (Webpack) didn't make the 'treeshaking', thus the NodeJs logic was imported into the bundle for browser process logic. 🤷‍♂️

electron-builder installer: A JavaScript error occurred in the main process, Error: The specified module could not be found

I am using electron 11.1.0 and electron-builder 22.10.5
I created a installer for my electron app which is using native module using electron-builder and its working fine on my laptop but on my friends laptop I am getting error
A JavaScript error occurred in the main process
Uncaught Exception:
Error: The specified module could not be found.
\\?C:\some\path\My-Electron-App\resources\app.asar.unpacked\node_modules\obs-studio-node\obs_studio_client.node
I have checked C:\some\path\My-Electron-App\resources\app.asar.unpacked\node_modules\obs-studio-node\obs_studio_client.node file exists.
I guess its asar related issue with native module. I tried by adding "asarUnpack": ["**/*.node"] in my build config but does not help.
How can I fix this problem?
I could not get this working with asarUnpack. Instead I use
"asar": false
Increases the bundle size, but at least it works.

Cannot run Jest in WebStorm

I am getting an error message:
"C:\Program Files\nodejs\node.exe" C:\Users\keechan\Desktop\NodeJS\my-module\tests\sample.test.js
ReferenceError: describe is not defined
This issue only happens when I run via Run (ctrl+shift+F10).
When I run via the Debug Configuration I created, it works okay.
But considering if I have many test.js files and I want to be able to run each individual test suite, I do not want to have to make a debug configuration for each one of them.
I already tried:
installing #types/jest library under settings (Languages & Frameworks > JavaScript > Libraries)
added #types/jest as well as jest in my package.json
Also, I noticed that it is calling node.exe in:
"C:\Program Files\nodejs\node.exe" C:\Users\keechan\Desktop\NodeJS\my-module\tests\sample.test.js
Maybe this is the reason why? So how do I change it to use jest?
Thank you so much!
You are not running Jest, you run your spec file by passing it directly to Node.js using Node.js run configuration. Make sure to use Jest run configuration to start your spec

Loosing jest variable when using esm package

I followed this answer to make jest work with esm packages
node - using jest with esm package
But I get an error when I try to do jest.SpyOn because it said that jest is undefined.
I did some digging and notice that jest is present on my app.test.js file but not in the asserts.js
So I was wondering how can I get the jest variable on the assert file or can I import jest directly without relaying on injecting it like the default config

react-hot-typescript-webpack setup issue

I'm using this template to get my project setup: https://github.com/wmaurer/react-hot-boilerplate-ts
When doing npm start i get the error (eventually):
ERROR in ./src/index.tsx
Module build failed: TypeError: compiler.parseConfigFile is not a function
at ensureTypeScriptInstance (C:\Users\mslavsky\Desktop\react-hot-boilerplate-ts-master\node_modules\ts-loader\index.js:147:38)
at Object.loader (C:\Users\mslavsky\Desktop\react-hot-boilerplate-ts-
master\node_modules\ts-loader\index.js:365:14)
# multi main
webpack: Failed to compile.
Can anyone help please? I did npm install as well
I suggest you take a look this project: https://github.com/jquintozamora/react-typescript-webpack2-cssModules-postCSS
There you can see configuration for WebPack 2, React Hot Loader, TypeScript and React

Resources