Mocha Not Picking Up Tests in Sub-Folder - node.js

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

Related

Write a custom plugin for a bundler like parcel in a web app

I have my own nodejs web application which is using Parcel 2 to bundle the resources.
{
"name": "acme-web-app",
"version": "0.0.1",
"description": "",
"keywords": [],
"license": "",
"author": "",
"scripts": {
"build": "parcel build index.html",
"dev": "parcel index.html --open",
"start": "npm run build && npm run dev",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"#parcel/packager-raw-url": "^2.6.0",
"#parcel/transformer-sass": "^2.6.0",
"#parcel/transformer-webmanifest": "^2.6.0",
"parcel": "^2.6.0",
"sass": "^1.52.2"
}
}
All works fine - the js bundles end up in a dist folder.
I am contemplating whether it is possible to write a parcel plug-in within this project is executed when the build script is run.
Is it possible - or does every reference need to go through npm channels via devDependencies?
I haven't figured a starting point for this - and have not been able to find what I am looking for on google so hopefully, the question makes sense.
NOTE: I have only really built web applications with nodejs.

Mocha JS: running multiple test files: how to stop Mocha from lauching many Firefox instacnes at once?

I'm having a problem with running test with Mocha.
I have 14 tests - each in one .js file. I want to run them one by one:
Open Firefox
Run the test
Close Firefox
Run new Firefox instance
Run the test
etc.
Problem is, when I execute "npm test" I'm getting 14 Firefox instances immediately opened. The test are running one by one (when one is running the other 13 instances are just open and waiting for the test tu end), but I don't want all instances at once.
Is there any solution for that?
OS: Windows 10
package.json:
{
"name": "Tests",
"version": "1.0.0",
"description": "Tests XXX",
"main": "xxx.js",
"dependencies": {
"assert": "^2.0.0",
"firefox-profile": "^4.2.2",
"mocha": "^9.2.2",
"mochawesome": "^7.1.3",
"selenium-webdriver": "^4.0.0-rc-1"
},
"devDependencies": {},
"scripts": {
"test": "mocha --sort --no-timeouts --reporter mochawesome --reporter-options reportFilename=TestResults"
},
"author": "mchodakowski",
"license": "ISC"
}

Error - Can't find Jest - Describe , It ect

I had this problem going on for weeks and today I decided to investigate this matter. What I have noticed is that if I create a brand new project and install #types/jest, jest, supertest and just make a test folder with a dummy test file like dummy.test.js, I'm able to get describe, it ect ect.
when I compare this package.json file with my microservices package.json file, the only difference is that I have few dependencies in my microservices package.json.
here you can see the difference:
new node project
{
"name": "jest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "NODE_ENV=test jest --watchAll --verbose --coverage"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#types/jest": "^27.4.1",
"jest": "^27.5.1",
"supertest": "^6.2.2"
}
}
and this is my microservices package.json file:
{
"name": "auth",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "NODE_ENV=development nodemon src/index.js -watch",
"test": "NODE_ENV=test jest --watchAll --verbose --coverage"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.0.1",
"config": "^3.3.6",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.0.0",
"express": "^4.17.3",
"express-async-errors": "^3.1.1",
"helmet": "^3.21.1",
"http-status-codes": "^2.2.0",
"joi": "^17.6.0",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"moment": "^2.29.2",
"mongoose": "^6.2.10"
},
"devDependencies": {
"#types/jest": "^27.4.1",
"jest": "^27.5.1",
"supertest": "^6.2.2"
}
}
Honestly I'm not understanding what is going on here!
In my new project, I don't need to configure a tsconfig.json for this matter, so I don't see the logic with this problem.
I would appreciate any explanation and maybe with an example to this matter so I can understand why can't see jest properties in my microservices project and in my new project the properties appears without any problems.
After searching what is tsconfig.json and what it does, I finally understood that I had to include it in my microservices project so I can tell the typescript compiler of what file it should be aware of.
Because I have structure all my files in a src folder in the root, I had to tell the compiler to include ["./src/**/*] and exclude["./node_modules"] because if I don't exclude it, we will issue a compiling performance and finally had to make sure to inform the compiler to include #types/jest without being referenced in a source file, in my case: "types": ["jest"] and allowJs to true so my javascript files can be part of my program. This had done the trick.
Now I must figure out why when building a new project, I don't receive these errors without tsconfig.json file. If some knows the answer, I would appreciate it!!

Why devDependencies is not installed

I developed a package for example: testlab, and its package.json is:
{
"devDependencies": {
"mocha": "^2.0.0"
},
"name": "#aab/testlab",
"version": "2.6.0",
"description": "example for npm",
"main": ".\\dest\\main.js",
"dependencies": {
"gulp": "^3.9.1",
"gulp-changed": "^1.3.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"test"
],
"author": "aab <aab#exmaple.com>",
"license": "ISC"
}
Say I included mocha in devDendencies, and then I created a local directory called e.g. c:\example and used
npm install #aab/testlab --only=dev
to get my package under c:\example, but when I looked at c:\example\node_modules, I did not find mocha package is installed. I also tried other command like
npm install #aab/testlab
still no luck. I used NodeJS v4.6.0 and npm 4.0.2. Although nodeJS seems a little old, could any one help me that?
I have seen this happen only when NODE_ENV is set to PRODUCTION. Something else might be setting it.

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