How to disable Babel integration in Jest? - javascript

I have project which has code targeting both browser and node. It has .babelrc file for the browser code. But when I'm running tests for the node code using Jest it always reads the .babelrc file which is not required.
So I can somehow disable it?

You need to create an additional jest setting file for your node test. In this file set transform to an empty object. To use this file you need to call jest with the --config option pointing to your node jest setting.

Related

How to run jest from node with the '--expose-gc' flag, when I need to load config/babel/webpack etc?

When I try to run jest with garbage collection exposed (e.g. as suggested here with node --expose-gc ./node_modules/.bin/jest --runInBand --logHeapUsage) I get errors about cannot load module '#babel/plugin-proposal-nullish-coalescing-operator and a require stack that shows it's looking in my .pnpm directory under node_modules.
Normally I just run 'npm run jest' which my package.json lists as just 'NODE_ENV=test jest'.
We have a jest.config.js that I presume that jest normally just picks up, but if I put -- -c=jest.config.js into the node command above, it looks like it's assuming that's a test file name?
We have a mix of .ts and .js files, as we're moving to TypeScript, and a very long webpack.config.js and babel.conig.js file that I presume does a bunch of work to make modules 'available' to the tests automagically.
So I'm not sure how to proceed from here?

How to see output on VS Code with Typescript?

I am doing some code challenges using Typescript in VS code. When I try to run the code and see the output, I get "Code Language is not supported or defined". The language mode is set to Typescript React (I also tried just Typescript). And the file has a .tsx ending. Finally, I also did compile the file and make a duplicate .js version. Is there something I am forgetting?
You cannot run a typescript file the way you would run a javascript file. You will need to first compile your typescript file into javascript with the command below in the directory containing your .ts file:
npx tsc -w <your-file-name.ts>
The command above will create a <your-file-name.js> file in the same directory and keep watching for any realtime changes with -w option. Then you can run the .js file in the same directory with either node or nodemon like this:
node <your-file-name.js>
---UPDATE---
Having to input these two commands mentioned above tsc -w and node <your-file-name.js> every time you might get cumbersome after a few times, you can use the ts-node command which combines both of the commands stated above into one:
ts-node <your-file-name.ts>
Run npx tsc -w
And in another terminal run node filename.js
Judging by the fact that your file ends with .tsx you are probably using React. I will also assume that you used create-react-app to init your project.
If both of my assumptions are correct, try:
npm start
If you are new to both React and Typescript, I would also suggest taking a look into that tool as it creates a very decent starting point for you to start learning both. https://create-react-app.dev/

Cordova build fails if I set 'type: modules' in package.json. How do I resolve this, yet still carry out unit testing?

I've built a simple app for Android, using the Cordova framework. I have now now implemented a simple unit test (using Tape), which runs in Node. Since the files I'm testing use ES6 modules, in order to run the tests in Node, I had to add "type": "module", to my package.json file. But when I did that, my app would no longer build--the Cordova build command fails with the output below. What is the proper way to resolve this issue?
Is it possible to programmatically set "type": "module",? If so, I can perhaps set it only when unit tests are running.
Or is some part of my method flawed in this case? That is certainly a possibility. Maybe if I test in the browser (rather than in Node), this issue will be avoided?
Thanks.
Build Output
$ cordova build android
Unable to load PlatformApi from platform. Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\snarl\my-project\platforms\android\cordova\Api.js
require() of ES modules is not supported.
require() of C:\Users\snarl\my-project\platforms\android\cordova\Api.js from C:\Users\snarl\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\src\cordova\util.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename Api.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\snarl\my-project\package.json.
Unhandled error. ('The platform "android" does not appear to be a valid cordova platform. It is missing API.js. android not supported.')
In case anyone else gets stuck on this, here is the solution I used (more of a workaround I guess). I bundled all files needed for unit testing into one single JS file--bundle-test.js--using Browserify (with a Babelify transform). Then I ran that file from the Node command line: node test-bundle.js. My unit tests ran without issue, and I didn't have to set "type": "module" in package.json.

JavaScript: How to use WebStorm debugger with ava

This is not question, just answer:
Create run/debug configuration, type Node.js
Select your node interpreter
As node parameter insert your ava bin and parameter --verbose
For me it's: ./node_modules/.bin/ava --verbose
Select your working directory
Done, now you can debug
The magic is in --verbose, I have no idea why it works that way, but it does.
I have no idea why/how it works for you - configuration is definitely wrong.
With your configuration, --inspect-brk is passed to ava, not to Node.js, and thus treated as your application argument. You should have specified node_modules/.bin/ava as JavaScript file: in your Run configuration instead of specifying it as a Node parameter, to make sure that Node debug arguments are passed before the application main file. --verbose can be passed as application parameter.
See also https://github.com/avajs/ava/blob/master/docs/recipes/debugging-with-webstorm.md
Create a new node test runner with following configuration:
Node interpreter: whatever version of node that you are using that is compatible with your ava version
Working directory: ~/Documents/Work/projectRootDir
Javascript file: node_modules/ava/cli.js
Application parameters: -v outdir/testFile e.g. /dist/test/controllers/test.js
There you go, now you can run and debug AVA with the best javascript IDE instead of console logging!
I'm quite sure that vscode config will be quite similar

How can I use TypeScript compiler through nodejs?

I have a sample code and saved it to a file such as hello.ts
After installing nodejs on windows use below command for installing typescript
npm install -g typescript
How can I compile hello.ts with node.js directly?
When I install "TypeScript 1.6 in VS2015" and use tsc.exe don't have any problem but I want to use node.js instead of VS 2015 extension
Please guide me generate .js and .ds through Node.js
Run tsc in the command line, you'll have the help page. Compiling a script is easy, just tsc hello.ts in the folder containing your script, you'll get a hello.js file.
Please guide me generate .js and .ds through Node.js
You have two options:
Run tsc.js as a node script
Use typescript as an npm module
Run node tsc.js
This is the approach taken by some tools e.g grunt-ts. You basically just call spawn on the current process process.execPath passing in the other commands as args (-d).
One sample
Run TypeScript as a node module
If you are playing with the typescript compiler API highly recommend NTypeScript See the Readme for reasons.
The TypeScript compiler provides a simple function called transpile that you can use to get the expected output and then write it out to disk yourself.
PS: I have some docs on the TypeScript compiler internals here : https://basarat.gitbooks.io/typescript/content/docs/compiler/overview.html
My preferred way to get going is to use typescript-require.
this library basically adds nodejs support for typescript.
your index file should be a javascript file and it will look like this
require('typescript-require');
require('./src/main.ts');
and main.ts is a typescript file.
Here's a programatically way from TS/Node to run the equivalent of tsc from within a code itself (Note: this uses the TypeScript Compiler API):
https://gist.github.com/rnag/0d8fe2e72dc7b48743c13f9ca8837a4c

Categories