how to prevent mocha from exiting process with status 1 - javascript

I have written a simple node.js application and now I want to add some tests. For now I have only some example Tests to test my Gitlab-CI, SonarQube and Mocha, which i am using for testing.
Now my problem is, that I want to parse the results from mocha (reporter is the sonar-mocha-reporter). My problem is that mocha exits the process with status 1 if a test is failing. That means gitlab-ci is breaking the build and will not run to the end. So i can not parse the results like it should.
Is there a way to configure mocha to not break my build so that it only saves the results in this xml file?
I am starting mocha with:
./node_modules/.bin/mocha -R mocha-sonar-reporter --recursive --no-exit
and in my package.json i have the configuration:
"config": {
"mocha-sonar-reporter": {
"outputfile": "build/reports/tests/TEST-mocha.xml"
}
},

Perhaps can you just do:
./node_modules/.bin/mocha || true
If it fails, it will then return true, and be successful.

Add parameter silent to command line. For example:
npm run test --silent

Related

Why do I get "node: bad option" error when I pass a custom flag to nodejs command line, in a Angular 9 project

I am trying to add a custom flag in the npm start command, in an Angular 9 project, so that inside the local proxy server, I can intercept the proxy request and manipulate data. But I keep getting the "node: bad option" error. Cannot figure out why. Please help me. Below is my npm start command
"start" : "node -max_old_space_size=8192 --enable-mock ng serve --proxy-config proxy.config.js --host=0.0.0.0 --live-reload false";
then I just run npm start, then the "node: bad option: --enable-mock" error shows up in the console, I cannot even start the development server.
When passing args to a program, Node is strict:
No args: node [NODE_OPTIONS] [FILE]
Args: node [NODE_OPTIONS] <FILE> [PROGRAM_OPTIONS]
See? When passing args, you need to include a file (use . in this case for the project index.js.)

Why would a full suite of Jasmine tests fail when running smaller suites individually works?

I have a protractor configuration file with the following suite glob patterns:
suites: {
all: ['**/*.spec.js'],
ui: ['ui/**/*.spec.js'],
api: ['api/**/*.spec.js']
},
If I, on a Mac, run npm run protractor (with the default suite of all), the tests run fine.
If another person on the team, on a Mac, runs npm run protractor, the tests run fine.
If the other person on the team, on a Ubuntu VM on a Windows host:
runs npm run protractor, the tests die. Specifically, the first line of the onPrepare throws an error.
runs npm run protractor --suite=ui, the tests run fine
runs npm run protractor --suite=api, the tests run fine
runs npm run protractor --suite=ui,api, the onPrepare errors again
At this point, I'm wondering if the issue is the VM's nodejs resources when Jasmine initially traverses the spec files. There are 15k+ its in the full suite. The onPrepare working fine outside of suite all makes me think the actual thrown error was a red herring (it was a database call with the mysql package that threw a connection timeout).
I can make a guess this is related to how different OS read your ** path. I'd recommend to use path library to solve this. Make sure you install it and then
let path = require('path');
let specs = path.resolve('**/*.spec.js'); // it should be /your/working/direction/**/*.spec.js
console.log(specs) // to confirm your assumption
exports.config = {
suites: {
all: [specs],
ui: ['ui/**/*.spec.js'],
api: ['api/**/*.spec.js']
},
}
But there might be something else, that causes the problem

how to determine if gulp watch is running

I run gulp on server in background running
gulp &
but sometimes it fail down. So mi question is: Is there some command for gulp to ask if is running. Something like
gulp status
Thanks
There is nothing special in gulp to limit running multiple processes or alert you to an already running gulp process.
Use regular Unix techniques to check if a process is running. Use a supervisor like supervisord or runit to automatically restart processes.
#the program pidof will set the variable $? to either
#1 or 0 in bash depending on if it finds a process
#with that name. It will also print out all the matching
#process IDs to the command line
pidof gulp
echo $? #1 if there is no process called gulp
#0 if there is a process called gulp
if pidof gulp; then
echo 'gulp is alive';
else
echo 'we need some support over here!'
./node_modules/.bin/gulp watch &
sleep 3
fi

warning: Recursive process.nextTick detected

I have application, that I'm starting to work with, I'm just want to run it, but it crash. It use grunt, that run node server, it's Angular.js application. When I'm running grunt task that run the server and when I try to access the app from the browser, I've got warnings from grunt or node:
(node) warning: Recursive process.nextTick detected. This will break in the next
version of node. Please use setImmediate for recursive deferral.
lot of lines and finaly:
util.js:35
var str = String(f).replace(formatRegExp, function(x) {
^
RangeError: Maximum call stack size exceeded Use --force to continue.
Aborted due to warnings.
I've try to search in my application for for process.nextTick but it's in lot of places in node_modules directory, and not in src.
Is it possilbe to remove that warning so I can run the application? What code should I search for this recursive call?
UPDATE
I use ack and found that this line came from this file in 3 places:
$REPO/node_modules/express/node_modules/connect/node_modules/multiparty/node_modules/‌​readable- stream/node_modules/core-util-is/float.patch
$REPO/node_modules/grunt-browser-sync/node_modules/browser-sync/node_modules/connect/‌​node_modu les/multiparty/node_modules/readable-stream/node_modules/core-util-is/float.patc‌​h
/usr/lib/node_modules/bower/node_modules/decompress-zip/node_modules/readable-s‌​tream/nod e_modules/core-util-is/float.patch
But it's not js file.
This might be a grunt issue. Grunt will vomit if there's repetition in your naming conventions.
This will break:
grunt.registerTask('foo', [ 'foo']);
This will not:
grunt.registerTask('foo', [ 'bar']);
Check out this SO post: grunt throw "Recursive process.nextTick detected"
npm dedupe solved it for me. Basically it reduces package duplication:
Searches the local package tree and attempts to simplify the overall structure by moving dependencies further up the tree, where they can be more effectively shared by multiple dependent packages.
More information
In my case I got the following warning before this error thrown:
Running "watch" task
Waiting...
Warning: watch ENOSPC
(node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.
This error indicates that number of resources it tries to watch is higher that the limit for this user. That's why running as root user (which doesn't have these limits) works fine. But this is not a solution.
Find out what is limit for your user in Linux:
sysctl --all | grep watches
Try to increase number of watches for your current user:
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
This should do the trick.
As posted by me here: grunt throw "Recursive process.nextTick detected"
Alternative solution: check your watch for an empty file argument.
Here's an excerpt of my gruntfile
watch: {
all: {
options:{
livereload: true
},
files: ['src/scss/*.scss', 'src/foo.html',, 'src/bar.html'],
tasks: ['default']
}
}
In my case, I could recreate the original poster's error on demand with the empty argument above.

grunt task: start mongod if not running

I want to write a grunt task to start the process mongod if the server is not already running. I need a mongod process running, but also need grunt-watch to work later in the task flow.
This question explains how to start mongod using grunt-shell ... the accepted answer is blocking, and the async version will spawn a new server even if one exists.
Is there a way (e.g. shell script) to start mongod only if it is not running, without blocking the rest of the grunt task flow?
Thanks
Here's a cleaner version
Store this as startMongoIfNotRunning.sh in same location as Gruntfile:
# this script checks if the mongod is running, starts it if not
if pgrep -q mongod; then
echo running;
else
mongod;
fi
exit 0;
And in your Gruntfile:
shell: {
mongo: {
command: "sh startMongoIfNotRunning.sh",
options: {
async: true
}
},
}
Edit - original version below
Ok - I think this is working properly...
create a shell script which will start mongod if it's not running... save it somewhere, probably in your project. I named it startMongoIfNotRunning.sh :
# this script checks if the mongod is running, starts it if not
`ps -A | grep -q '[m]ongod'`
if [ "$?" -eq "0" ]; then
echo "running"
else
mongod
fi
You may have to make it executable: chmod +x path/to/script/startMongoIfNotRunning.sh
Install grunt-shell-spawn : npm install grunt-shell-spawn --save-dev
Then in your Gruntfile add this:
shell: {
mongo: {
command: "exec path/to/script/startMongoIfNotRunning.sh",
options: {
async: true
}
},
}
(If you're using yeoman, using <%= yeoman.app %> didn't work because those paths are relative to the whole project, so you get something like 'app' instead of the whole path to the script. I'm sure you could get it working, I'm just not aware how to get the path to )
If you just execute the task grunt shell:mongo mongod will start but I wasn't able to close it using grunt shell:mongo:kill. However, assuming you're using a blocking task later (I'm using watch) then it should automatically be killed when you end that task.
Hope this helps someone!
I found your solution really helpful, but actually wanted to kill mongod when restarting grunt server. So I got this:
#!/bin/sh
# this script checks if the mongod is running, kills it and starts it
MNG_ID="`ps -ef | awk '/[m]ongod/{print $2}'`"
if [ -n "$MNG_ID" ]; then
kill $MNG_ID
fi
mongod
which works really nice on my mac. And my grunt file looks like this:
//used to load mongod via shell
shell: {
mongo: {
command: './mongo.sh',
options: {
async: true
}
}
}
So my mongo.sh is in the same location as my Grunfile.js
Cheers
The other two answers are correct. However for completeness here is the equivalent batch script on Windows. Save the following as startMongoIfNotRunning.bat:
tasklist /fi "imagename eq mongod.exe" |find "=" > nul
if errorlevel 1 mongod
If there is a task running called mongod.exe then the = character should appear in the output - hence if it is not running the = character will not be found and the errorlevel variable will be set to 1.
The rest is the same as #MaxBates answer.

Categories