Mocha and ts-node gives 'Unsupported Args' - node.js

In my package.json I have defined my test script:
"scripts": {
"test": "mocha --require ts-node/register ./test/**/*.ts",
"build": "npx tsc"
}
When I run npm test I get back the result:
> mocha --require ts-node/register ./test/**/*.ts
error: Unsupported Args: --require ts-node/register ./test/**/*.ts
It seems to somehow be the command interaction. Even if I run it manually node .\node_modules\mocha\bin\mocha --require ts-node/register "./test/**/*.ts" it fails with the same message. If I remove --require ts-node/register it runs, but fails when running the test on import statements because my test files are Typescript files.
How do I make mocha work with ts-node?

My package.json file has it in scripts
"scripts": {
"test": "./node_modules/mocha/bin/mocha -r ts-node/register tests/test.ts"
},
And after this I can run npm test and everything seems good.
Versions in devDependencies:
"devDependencies": {
"#types/chai-http": "^4.2.0",
"#types/expect": "^24.3.0",
"#types/mocha": "^9.0.0",
"#types/chai": "^4.2.18",
"#types/node": "14.14.30",
"chai": "^4.3.4",
"mocha": "^9.1.3",
"ts-node": "^9.1.1",
"typescript": "^4.4.4"
}

I was unable to find an answer to how to solve it with the command line, but I circumvented the problem by moving the options into package.json
"scripts": {
"test": "mocha"
},
"mocha": {
"extension": ["ts"],
"spec": "test/**/*.ts",
"require": "ts-node/register"
}
Update: I found the error. I was moving some code without test to having test. The logger was defined in a main.ts file which had some code that ran that was not behind any guard. In fact the error Unsupported Args was my own error message.
The act of referencing a winston logger caused the code in main.ts to run. For some reason this error does not happen now that the mocha options are in package.json even though the logger is still referenced which I find confusing. I am not sure why whether the options are directly in the command line, or they are in the package.json causes such a behavior difference. Regardless, the lesson learned is never reference into your main "running" module.

Related

How to run Nodemon with TypeScript in debug mode in WebStorm

I have a very basic Express server setup with NodeJS/Express + Nodemon. I'm using WebStorm as my IDE.
When running the debug/inspect script, my application is throwing a compile-time error. Following the instructions here, and cognizant of the post here, my (simplified) package.json looks like this:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/app.ts",
"debug": "nodemon --inspect=127.0.0.1:9229 src/app.ts"
},
"devDependencies": {
"#types/express": "^4.17.9",
"#types/node": "^14.14.19",
"nodemon": "^2.0.6",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
}
When I run npm run debug I receive the following error:
I've also tried the following, all with the same result:
"debug": "nodemon --inspect src/app.ts"
"debug": "nodemon --inspect=0.0.0.0:9229 src/app.ts"
Any ideas why this error is occurring?
The command npm run dev does not generate an error. Upon further investigation, nodemon automatically substitutes ts-node when targeting a .ts file. Ts-node, in turn, recommends debugging by registering ts-node and running the server with the node command.
If you need to use advanced node.js CLI arguments (e.g. --inspect),
use them with node -r ts-node/register instead of the ts-node CLI.
You can modify your script as follows:
"debug": "nodemon --exec \"node --inspect-brk=0.0.0.0:9229 --require ts-node/register src/app.ts\""
to make sure that the --inspect-brk is passed to Node.js and not to ts-node

Mocha Not Picking Up Tests in Sub-Folder

I am setting up some tests in my Node project using Mocha with Chai. I have numerous tests running successfully, and now I'd like to group them a little more logically. Right now all tests are located in one "test" folder, which is at the root of the project.
I'm noticing that when I create a sub folder within that "test" folder, and put a test in there, that it never gets run by Mocha. Any idea why this is happening? My understanding is that Mocha runs tests within any folder named "test", as well as any sub-directories within that "test" folder.
My package.json look like this:
{
"name": "event_runner",
"version": "1.0.0",
"description": "",
"main": "server.js",
"directories": {
"lib": "lib"
},
"scripts": {
"test": "mocha || true",
"start": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"agenda": "^2.1.0",
"agendash": "^1.0.0",
"express": "^4.17.1",
"mariadb": "^2.1.1",
"mongoose": "^5.7.3"
},
"devDependencies": {
"chai": "^4.2.0",
"chai-datetime": "^1.5.0",
"mocha": "^6.2.1"
}
}
To get tests to run I was just typing mocha from the command line. This works ONLY when all tests are in one root folder. I did find a solution to run all tests, including those in sub-folders. On my mac I have to run this from the command line:
mocha "./test/" --recursive
BONUS: To exit after running tests, add the --exit flag:
mocha "./test/" --recursive --exit

How to setup create-react-app with webpack-dev-server

I'm trying to setup my create-react-app instance with webpack-dev-server.
This is my package.json file
{
"name": "reactgs",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-scripts": "1.0.14"
},
"scripts": {
"start": "react-scripts start",
"build": "webpack-dev-server --entry ./node_modules/react-scripts/bin/react-scripts.js --output-filename ./dist/bundle.js --inline --progress",
},
"devDependencies": {
"webpack-dev-server": "^2.9.1"
}
}
I don't have a webpack.config.js file.
When I run
npm run build
The output appears as
The following line runs fine.
npm start
I also have webpack setup fine on the same machine for a different project.
Does anyone know how to properly configure webpack for create-react-app?
npm start does the HMR for you out-of-the box. It will host your application using webpack-dev-server --hot itself. You just need to hook the HMR code to tell webpack what to watch and reload as necessary.
This GitHub conversation offers a pretty decent simple solution for hooking HMR (https://github.com/facebookincubator/create-react-app/issues/2317) with and without Redux.

"Error: spawn git ENOENT" when running react-scripts test

I have a project that I created using the create-react-app. When I first created the project the tests ran fine. I decided to run the tests after not running them for a while (I still haven't written any so the generated test class is all I have) and now I'm getting the following error:
Determining test suites to run...Error: spawn git ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32)
at onErrorNT (internal/child_process.js:344:16)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
Git is definitely on the path, I can run git --version from the command line and it outputs as expected. I get the same error if I run in cygwin or the windows command line.
My package.json looks like:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"immutable": "^3.8.1",
"react": "^15.5.4",
"react-dom": "^15.5.4"
},
"devDependencies": {
"react-scripts": "0.9.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
EDIT: After some playing around I've discovered that my tests only fail when the project is in a git repository. If I take a copy of the project, without the '.git' folder then run the tests from the copy they all run fine.
I eventually figured out what was causing this. Something about my local setup is causing an error when git is launched as part of the Jest watcher (still not clear what). From the create-react-app docs:
By default, when you run npm test, Jest will only run the tests related to files changed since the last commit. This is an optimization designed to make your tests runs fast regardless of how many tests you have. However it assumes that you don’t often commit the code that doesn’t pass the tests.
Source
Running the tests as if they were running in a CI environment with set CI=true&&npm test bypasses the git functionality and I can run my tests. More information here
There is also an open bug in the jest project: https://github.com/facebook/jest/issues/3214

Watcher automation in Mocha

I would like to achieve automation of motcha --watcher feature using package.json file without globally installing mocha.
One of npm features is to allow add custom scripts into npm command. Previously I configured test runner successfully and I can type in bash now:
npm test
Everything works fine, so I would like also do something similar because
./node_modules/mocha/bin/mocha --watch app.js test.js"
is not too efective.
My goal is to run mocha watcher by typing in bash:
npm watch
Unfortunately watcher doesn't run - instead I see standard output of npm command without parameters. It looks like my custom script wasn't registered by npm.
Here is my actual package.json file
{
"name": "screencast",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha test.js", // works properly after typing 'npm test'
"watch": "mocha --watch app.js test.js" // Syntax looks ok, but command 'npm watch' d
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.10.6"
},
"devDependencies": {
"mocha": "^2.0.1",
"supertest": "^0.15.0"
}
}
Anyone had this issue before?
For a 'custom' script like watch, you have to do npm run watch instead of npm watch

Resources