Watcher automation in Mocha - node.js

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

Related

How can I execute a bin with yarn?

I have the following package.json and I'd like to run the bins "build" and "run":
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"bin": {
"build": "./bin/build.js",
"dev": "./bin/dev.js"
}
}
I've tried:
yarn run build
and I get
error Command "build" not found.
I've also tried:
yarn build
but the same thing happens:
error Command "build" not found.
It's propably not the right way to run bins. But then again, what is the right way to run bins with yarn?
Your package isn't installed.
When Yarn (and NPM) installs your package, it adds the commands under node_modules/.bin/, e.g. node_modules/.bin/build. Running yarn build would (if it doesn't find a matching script in the current package) look for a build in this .bin, then traverse upwards through the filesystem, looking for other node_modules/.bin/build's.
If your build script is only meant to be run while developing that specific package, add it as a script (see example here). It would more or less look like this:
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"scripts": {
"build": "node ./bin/build.js",
"dev": "node ./bin/dev.js"
}
}
Do not need relative path added:
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"scripts": {
"build": "build.js",
"dev": "dev.js"
}
}
The hashbang comment specifies the path to a specific JavaScript interpreter that you want to use to execute the script.
For example, helloWorld.js in ./node_modules/.bin:
#!/usr/bin/env node
console.log("Hello world");
You have a typo in your package.json. Where it says bin: it should say scripts:
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"scripts": { // <-- here
"build": "./bin/build.js",
"dev": "./bin/dev.js"
}
}

sh: 1: cucumber.js: not found

Trying to use this tutorial here:
https://github.com/lykmapipo/nodejs-cucumber-sample
The output to nvm current is: v10.12.0.
The output to npm --version is: 6.4.1>
I get the error below once I invoke npm test:
> nodejs-cucumber-sample#0.0.1 test /home/gnuc/code/nodejs-cucumber-sample
> cucumber.js
sh: 1: cucumber.js: not found
I am not sure as to why this is the case. The $PATH includes /home/gnuc/.nvm/versions/node/v10.12.0/bin. And I have already used npm install cucumber -g and npm install cucumber
Make sure that your package.json file includes this: "test": "cucumber-js"
So that it looks something like this:
{
"name": "hellocucumber",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": **"cucumber-js"**
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"cucumber": "^5.1.0"
}
}
So, you need to actually call the npm package/library. I have the following defined in my package.json:
"scripts": {
"test": "node ./node_modules/.bin/cucumber-js"
},
you can also add some ---tags in this call.
"scripts": {
"test": "node ./node_modules/.bin/cucumber-js --tags #RegressionTestSuite"
},
this will run any feature files that have #RegressionTestSuite at the top
Also, I have an output/results file created with a time stamp.
"scripts": {
"test": "node ./node_modules/.bin/cucumber-js --tags #RegressionTestSuite --format json:./results/log_new_`date +%Y-%m-%m__%H-%M`.json""
},
I hope this helped.
node ./node_modules/cucumber/bin/cucumber-js
This command is working fine.
And you got sh:1: cucumber.js: not found error means first things please look out the path of cucumber.js

Why is Heroku not detecting my start script?

I have a Node.js application on Heroku. To start it, I need index.jsto be executed. To do that, I added a package.json file with a start script.
As I read in Heroku Node.js Support:
First, Heroku looks for a Procfile specifying your process types.
If no Procfile is present in the root directory of your app during the build process, your web process will be started by running npm start, a script you can specify in package.json...
When I define the start script to be node index.js and I deploy my app to Heroku, I don't see any Dynos in the resources tab.
My code:
package.json
{
"name": "node.js app",
"version": "1.4.0",
"description": "A node.js app.",
"main": "index.js",
"repository": {
"type": "git",
"url": "Node.js repository"
},
"author": "Realex78",
"license": "MPL-2.0",
"dependencies": {
"npm package": "^1.0.0"
},
"devDependencies": {
"npm package": "^1.0.0"
},
"scripts": {
"start": "node index.js",
"poststart": "node scripts/poststart.js",
"restart": "node scripts/restart.js"
}
}
Make sure that you aren't using a Procfile, as Heroku gives it priority over package.json!
I.e. even though I had updated my startup script in package.json:
"scripts": {
"start": "node --harmony server.js"
},
I had forgotten that my Procfile was setup like this:
web: node server.js
Therefore my startup script in package.json was being ignored! The fix was to update my Procfile (to in this case include the --harmony param).
Note that I did not have to wait 24 hours for a successful deploy; it worked immediately.

Running a custom built npm package not working

I am building an npm library and published it to npm.
After publishing I am trying to install the library globally.
But it's not working, looks like it's trying to open the file directly
My package.json looks like as follows:
{
"name": "lssomename",
"version": "1.0.4",
"description": "Test",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"run": "node index.js"
},
"author": "Test",
"license": "ISC",
"dependencies": {
"command-line-args": "^5.0.2",
"fs": "0.0.1-security",
"ncp": "^2.0.0",
"rimraf": "^2.6.2",
"zip-folder": "^1.0.0"
}
}
As soon as I try to run it throws the following error
/home/thabung/.nvm/versions/node/v8.5.0/bin/lssomename: line 1: $'\r': command not found
/home/thabung/.nvm/versions/node/v8.5.0/bin/lssomename: line 2: syntax error near unexpected token `('
'home/thabung/.nvm/versions/node/v8.5.0/bin/lssomename: line 2: `const log = require("./logger");
The same error comes if try to run directly the index.js file, meaning
when I try
./index.js
instead of
node index.js
Ok the issue was, I need to add an indicator in the begining of the file,
also known as shebang for Unix like systems like as follows
#!/usr/bin/env node
in the beginning of my index.js file & it started working after that.

running a command after install dependencies using npm install

I have a package.json file like this
{
"name": "E2E",
"version": "1.0.0",
"description": "AngularJS E2E testing",
"main": "conf.js",
"scripts": {
"postinstall": "node_modules/protractor/bin/webdriver-manager update",
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "ISC",
"devDependencies": {
"protractor": "^2.2.0"
}
}
when running command npm install after protractor is installed its throwing error
node_modules/protractor/bin/webdriver-manager update
'node_modules' is not recognized as an internal or external command, operable program or batch file
Ok found the fix, I need to run it as node command like this
"postinstall": "node node_modules/protractor/bin/webdriver-manager update",
Try prepending the path to executable with a dot followed by a slash:
./node_modules/protractor/bin/webdriver-manager update
The problem is that you need to be in the folder where that command is installed before you call it. Assuming you are using Windows, this can be solved by running a simple batch file:
#echo off
call npm install -g protractor
call npm install
cd C:/Users/%USERNAME%/AppData/Roaming/npm/node_modules/protractor/selenium/
call webdriver-manage update
You should be able to run a batch file from anywhere. In fact, the entire Protractor testing process can be automated with a batch file. You just need to add Grunt, load-grunt-tasks, grunt-protractor-runner, jasime, and protractor-jasmine2-html-reporter to your package.json:
{
"name": "yourproject",
"version": "0.0.1",
"dependencies": { },
"devDependencies": {
"grunt": "~0.4.1",
"load-grunt-tasks": "~1.0.0",
"grunt-protractor-runner": "~2.1.0",
"jasmine": "~2.3",
"protractor-jasmine2-html-reporter": "~0.0.5"
},
"engines": {
"node": ">=0.12.0"
}
}
After you configure Protractor and writing some tests, you can then call the whole process with one simple batch file:
#echo off
cd %CD%
#echo running tests
call grunt
#echo Opening test results in browser
start "" %CD%\tests\reports\index.html

Resources