Is there an alternative to jest-environment-jsdom for jest tests using the window object - jestjs

I'm writing jest unit tests of some angularjs code that expects the windows object. The unit tests are failing because of this error:
Reference Error: window is not defined
The default mode for jest is "node". If I want to use "jsdom" (which would add a window object), then I need to install jest-environment-jsdom. However, when trying to install jest-environment-jsdom, I encounter node-gyp errors. I looked up node-gyp installation help and found:
https://github.com/nodejs/node-gyp#on-windows
Which tells me that I need to install VS Build tools. This is a huge installation. I'm looking for an alternative solution that won't require other dependencies.

Related

CodeMix/Eclipse cannot find 'react/jsx-dev-runtime'

I'm completely new to React, Node, NPM, and CodeMix in Eclipse. I'm a veteran java developer and so want to use eclipse over VSCode for familiarity. I'm following this tutorial:
https://www.genuitec.com/react-tutorial-getting-started/
I get to step 5 and run
react-scripts start
The result:
./src/index.js
Module not found: Can't resolve 'react/jsx-dev-runtime' in 'C:\temp\workspace\CCFPortal\src'
I created my project via the new project to create a React project. The wizard added all the standard code. Bringing up the terminal gave me some issues because the terminal doesn't seem to set the path to bring nodejs.exe into scope, so I did it manually in the terminal window. Adjusting the properties in window->preferences terminal+ doesn't seem to ever take effect.
I did the following in npm (in addition to the steps in the tutorial) because there were two 'warnings' in the markers view that suggested it. As near as I can tell, all commands were successful whether from the tutorial or these two (though it does flag some 'vulnerabilities' in npm that I force fixed).
npm install #types/react
npm install #types/react-dom
It is indeed true that there is no react/jsx-dev-runtime under that src folder. There's NOTHING there but the src. But I'm unclear why it wasn't there given that I ran npm install and the other things suggested in the tutorial.
Can someone help explain my pathing issue in the terminal and especially why npm is doing this when I run react-scripts start?

Node.js native addon module (tfjs_binding.node) can not be found

I am writing an Electron application and would like to integrate my AI. For this I have installed #tensorflow/tfjs-node version 3.0.0. When I want to execute my code (const tf = require("#tensorflow/tfjs-node");) the following error message appears in the console:
Uncaught (in promise) Error: The Node.js native addon module (tfjs_binding.node) can not be found at path: C:\...\simplePhishNew\node_modules\#tensorflow\tfjs-node\lib\napi-v6\tfjs_binding.node.
Please run command 'npm rebuild #tensorflow/tfjs-node build-addon-from-source' to rebuild the native addon module.
The Console is tell me:
node-pre-gyp info This Node instance does not support builds for N-API version 7
The Solution,
Please run command 'npm rebuild #tensorflow/tfjs-node build-addon-from-source' to rebuild the native addon module.
doesnt work and is showing the following error message.
CPU-windows-3.0.0.zip
* Building TensorFlow Node.js bindings
symlink ./lib/napi-v7 failed: null
#tensorflow/tfjs-node#3.0.0 C:\...\node_modules\#tensorflow\tfjs-node
Thanks for the effort
I did it guys...
Step 1:
Stop crying
Step 2:
Call the boss again and take back the notice of termination
Step 3:
Electron need a specific node version
https://github.com/electron/releases
For example Electron v. 11.3.0 needs node 12.18.3
Step 4:
Ask yourself why you actually got the job

node 6.11.3 NODE_EXE not found in IntelliJ

I've updated my project to use node 6.11.3. When I now try to start a npm-script by using the Run-configurations provided from IntelliJ, I always receive the following error:
Error: Failed to replace env in config: ${NODE_EXE}
Important: This error appears only if I start npm from within my project. If I use the windows cmd, the error doesn't appear.
What could possibly have changed between node 6.11.2 and 6.11.3? Because with the prior version, everything worked fine.
A workaround for me is to add the NODE_EXE variable to my run configuration, but In my opinion, that shouldn't be needed, because it worked in 6.11.2 too.
Looks as if you have ${NODE_EXE} variable set in one of your npmrc files (see https://docs.npmjs.com/files/npmrc#files), and it can't be properly expanded for some reason when you run your script in the IDE.
is the issue specific to certain project?
how many npm versions do you have installed? Please check that npm chosen in Node.js Interpreters dialog is the same as you use in cmd shell?
please create an env.js file with console.log(process.env) and try running it via npm ("env" : "node env.js") in both cmd console and WebStorm - what is the result?

atom + mocha on windows = spawn mocha ENOENT

My goal is to run mocha unit tests by Atom, which is installed on Windows and also my src code resides. this should work independently from my Meteor App which is running on a different (Linux) machine.
Basically my setup is like this:
I have my repo and sourcecode:
c:\Users\Me\repos\meteor
My tests are inside:
c:\Users\Me\repos\meteor\tests
I have Node:
c:\Program Files\nodejs
installed with "npm i -g mocha --save-dev"
And i try to use this package https://github.com/Tabcorp/atom-mocha-test-runner but i can switch to another package if necessary.
What I've tried so far:
I edited my settings for the atom-mocha-test-runner:
Mocha command: C:\Program Files\nodejs\node_modules\mocha\.bin\mocha
Mocha command: C:\Program Files\nodejs\npm mocha
But each time i try to run my test via dropdown menu (Run Mocha Test), i get this error:
Mocha Test Results:
Node binary: C:\Program Files\nodejs\node.exe
Root folder: C:\Source\Repos
Mocha command: undefined
Path to mocha: mocha
Debug-Mode: false
Test file: tests\unit\first.js
Selected test: should return url
Failed to run Mocha
spawn mocha ENOENT
Anyone know what i miss or do wrong?
Still having no idea about why the package isn't working, I'm going to give a cop-out answer. If we figure out how to make it work, you can accept that answer instead. process-palette gives you the ability to run highly specific command-line instructions from Atom commands. Here's an example of a command that runs mocha in the project path for the current file with the same hotkey and also conveniently organized into its own menu item:
The disadvantage of this approach is that you have to know how to use the external program yourself. Packages like mocha-test-runner are designed to remove that need from the user, but as we can see here, sometimes the package doesn't know what it needs to be doing. The disadvantage is mitigated by the fact that you only have to learn the command for long enough to set up the configuration to run it, and from that point on it's very easy.
Advantages versus other packages include the ability to precisely control what's going on. Say you have multiple top-level folders in the current project, and they have different test suites. A package like mocha-test-runner can get the path from the active file, or from the project. If the developer chose to grab the project path, then you're going to have trouble running individual test suites. With the configuration I've shared, the command will always be run in the absolute path of the current file's project folder, so the tests will be run for whichever file you're working on at the time.

PhantomJS from Node on Windows

I have written a Electron application using Node, Electron Boilerplate, and phantom. It works perfectly fine for me on my linux machine, I copied the source over to Windows 10, and ran with npm start, and all goes smoothly.
However, when I try to build the application with the boilerplate module using npm run release, things go a little less smoothly. I can install and open the application just fine, but when I click the button that activates the phantom module, the windows goes all white and nothing happens. I was able to logs some errors with the dev tools.
First, I have:
C:\...\dist\win-unpacked\resources\app.asar\node_modules\phantom\lib\phantom.js:361
Uncaught (in promise) Error: Error reading from stdin: Error: write EPIPE(…)
I did some research into similar issues, namely here, and it seems to me the issue is starting the child process, PhantomJS, with the npm module phantom. Originally, I was using a WPF application I wrote in C# to start the process, and that worked just fine. This leads me to believe that the phantom module is the culprit.
So I tried swapping out the npm phantom module for horseman, but got similar results:
Unhandled rejection HeadlessError: Phantom immediately exited with: 4294967295
at ChildProcess.immediateExit (C:\...\dist\win-unpacked\resources\app.asar\node_modules\node-horseman\node_modules\node-phantom-simple\node-phantom-simple.js:153:23)
at ChildProcess.g (events.js:286:16)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:204:12)
Here is a shot in the dark. I am not positive this will solve your issue but here it goes:
GYP and miss-matched binaries
Phantom and many other node modules use binaries built for the specific OS that it will be running on. Sometimes in your npm log files you will see references to node-gyp. Node-gyp simply helps to build native add-on's in node modules. When the binaries are built they are usually built against, among others, three main parameters, the operating system, cpu architecture and version of node that is doing the installation.
I think you need to rebuild phantomjs to the version of node Electron is using. Most of the time the node version you have installed on your machine and the node version running in Electron are not the same. Electron does its best to keep up, but there is always a little lag because of the amount of work and testing required to keep up-to-date.
When you install phantom by running npm install phantom it will assume it needs to install or build the binaries for the node version your machine is using. Then when your Electron app tries to run phantom it tries to call the binary of the Electron's node version. When it isn't there the child process immediately exits with an error.
How to fix
Luckily, there are other people out there that have figured out how to fix this issue and have created a great tool to help generate the correct binaries.
Enter electron-rebuild:
https://github.com/electron/electron-rebuild
Electron-rebuild can be run in the command line, and it will rebuild all of your native modules to the version of Electron your project is using.
To install:
npm install electron-prebuilt
To use (in Windows):
.\node_modules\.bin\electron-rebuild.cmd
This should be enough to put the correct binaries in the right place.
Other thoughts
Sometimes you can use a package that uses a dependency called node-pre-gyp. E.g. sqlite3. There is a known issue I ended up running into when trying to rebuild my packages for Electron. Basically, in order to avoid this issue (if you run into it) just append --pre-gyp-fix to the above command.
Tangent for those who run into the pre-gyp-fix issue
One more thing on the pre-gyp-fix: If one or more of your dependencies depends on one of the modules that need the pre-gyp-fix then they will be looking for the binary in the wrong place even if they are running in Electron. All of the pre-gyp binaries are stored in a folder similar to this:
.\node_modules\sqlite3\lib\binding
In my current project I have three folders here, one for Electron-v1.4, and two for node-v46 and node-v50. (hack alert) In order to have sqlite3 work with my other dependencies I copy the binary found in the Electron-v1.4 folder and put it in both node-v* folders. That way when running in Electron, all dependencies are running the correct binaries even though they are looking for them in the wrong place. (end hack alert)
Conclusion
There is no way I can be sure this has anything to do with the issue you are seeing. But it is worth a shot to see if it fixes your problem. If not then at least I hope I can help someone else experiencing the same issues I ran into.

Resources