How to transform a npm test script to a grunt task? - javascript

I have the following script with my nodeJS.
"scripts": {
"test": "babel-tape-runner 'test/**/*.js' | tap-notify | tap-diff "
}
If I run my tests with npm test everything runs properly, but I would prefer to have it all in a gruntfile. Is there a way to configure grunt to run the tests?

Related

'npm run test' works, but 'jest --coverage' command does not work

I have MonoRepo project (using Lerna) which consists of multiple packages, and one of them is a React application.
Within the React project package, I utilise Jest for unit testing. However, when I run the jest --coverage command on the WebStorm console, it returns me the following error:
zsh: command not found: jest
Strangely enough, if I use npm run test, the test runs, and my test coverage report is generated. Here is part of my package.json:
"scripts": {
"test": "jest --coverage",
// other stuff
},
As you can see, npm run test runs jest --coverage, which is the exact command I initially entered. Why won't the jest command work on its own?
You need to run it directly from your node_modules like this:
./node_modules/.bin/jest --coverage
See https://jestjs.io/docs/en/getting-started#running-from-command-line for more information
The most straightforward solution is to install jest globally with:
npm i -g jest

Why I am not able to use mocha through nodemon?

I am trying to run test cases using mocha framework. I am trying to run it through nodemon, but getting an error.
I have installed nodemon already through npm install nodemon and similarly with mocha.
How do I run my project through nodemon so that my test cases show the result using mocha framework?
In the Command Prompt, my test cases are not running for whatever I do with nodemon.
Lets assume you have an entry like following in your package.json -
scripts": {
"start:test": "mocha test/ --recursive --exit"
}
To run the mocha test using nodemon please use the following command:
nodemon --exec "npm run start:test"
"test": "nodemon --exec 'mocha -R min'"
then: npm run test

Specifying the location of mocha.opts when running mocha with nyc?

Is it possible to specify the location of mocha.opts when running nyc mocha? I'd like to have it in the root project folder. I tried this in my npm script:
"scripts": {
"test": "nyc mocha --config ./mocha.opts"
},
That did not work. I did get a test case working for the setup described here however, with mocha.opts in the test directory.
mocha --opts <path_to_opts_file>

How to run Jasmine tests in watch mode for TypeScript

I have a Node.js app using TypeScript and now I want Jasmine to run tests automatically each time I make changes in .ts files. So I'm just trying to find an appropriate command to be run as npm test in command line or a package that can watch my .ts files compile them on changes and run jasmine. Does anybody know a solution for it?
The easiest way I found is
installing dependencies: npm install --save-dev jasmine-ts nodemon
initializing jasmine: node_modules/.bin/jasmine-ts init
In the package.json:
"scripts": {
"test": "nodemon --ext ts --exec 'jasmine-ts \"src/**/*.spec.ts\"'"
}
Edit: the above solution doesn't work as of the 11th of Apr, 2019. I published a modified working example at https://github.com/erosb/ts-node-jasmine-example
This may be done with two commands launched in separate terminals. Assuming packages are installed in global mode.
First command launches TypeScript compiler in watch mode:
tsc --watch
The second starts nodemon that watches .js files and restarts on changes. Each time it executes jasmine test runner:
nodemon --ext js --exec 'jasmine JASMINE_CONFIG_PATH=jasmine.json'
This solution is fast enough though it also has a drawback of running in two terminals. So it is not ideal but the best I've found so far.
As a result scripts section in package.json looks like:
"scripts": {
/* ... */
"watch": "tsc --watch",
"test": "nodemon --ext js --exec 'jasmine JASMINE_CONFIG_PATH=jasmine.json'",
"devstart": "nodemon ./bin/www"
},
devstart also works in couple with watch restarting server each time .ts files are changed (after they are compiled to .js).
You might consider using jasmine-node. I don't think that jasmine itself has a watch option.
npm i -g jasmine-node
Assuming that your test command in your package.json scripts block is something like this:
"scripts": {
...
"test": "jasmine some-directory-or-glob-pattern"
...
}
Use jasmine-node and add the --autotest and --watch flags to that command:
"scripts": {
...
"test": "jasmine-node --autotest --watch some-directory-or-glob-pattern"
...
}
Previously described methods either did not work, or were slow to compile code. Here is my attempt to solve this, both fast and convenient, works great for me. The only downside is that jasmine would not know which tests are affected by TS recompilation and would run all the tests.
yarn add tsc-watch --dev
yarn run tsc-watch --onSuccess "yarn run jasmine --config=jasmine.json"
NPM version:
npm -i tsc-watch
npm run tsc-watch --onSuccess "npm run jasmine --config=jasmine.json"
In my case I needed to correctly map TS paths. The full command looks like this:
yarn run tsc-watch --onSuccess \
"node -r tsconfig-paths/register node_modules/jasmine/bin/jasmine \
--config=jest/jasmine.json --require=dist/jest/setup.js $targetFile"
jasmine.json
{
"spec_dir": "dist/src",
"spec_files": ["**/*.e2e.js", "**/*.unit.js", "**/*.spec.js", "**/*.test.js"],
"env": {
"random": false
}
}
Just an example, please adjust to your needs.
tsc-watch starts a TypeScript compiler with --watch parameter, with the ability to react to successful compilation and start tests.

How to use Grunt/Gulp with pm2?

pm2 is a great tool to manage node apps. How does it work with grunt/glup ?
I didn't find any useful clues after Googling for 20 minutes.
If I understand your question well, it seems you want to deploy your app.
Since pm2 0.9 deployment can be done with pm2 deploy see README.
In the case of grunt/gulp, I see two options:
You've your node_modules comitted. Using pm2 deploy run your gulp process from the post-deploy section:
"post-deploy" : "node ./node_modules/gulp/bin/gulp.js ./GulpFile.js && pm2 startOrRestart ecosystem.json --env production"
Using a basic script that will launch npm install for you, you could use the package.json to grunt/gulp:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"postinstall": "./node_modules/bower/bin/bower -q -s -f install && ./node_modules/gulp/bin/gulp.js"
},
My gulp generally needs bower to minify scripts so I left it only for example purpose.
You may combine the two options to let pm2 deploy install your npm scripts and have a postinstall script in the package.json.
Note that I'm using the relative path to the gulp module binary! It's just to avoid an issue if the global module is not installed.
Now, in my opinion to deploy an application in production it's better to simply have a git branch where everything is pre-gulped so that you only clone that branch and you're good to go. It also improves the deploy time, especially if you're running tests with gulp or grunt...
Hope that's clear enough!
The Reply may be late it must be usefull to others
On the command line do:
$ export NODE_ENV=production
will setup production environmental
$ grunt build
will create necessary min.js and min.css
$ pm2 start server.js
will load the server with pm2 , that its a package thats makes sure the node server will restart if an error and will log.

Categories

Resources