Running mocha on various packages with lerna - node.js

I have just started with a mono repo with multiple packages using lerna.
On all my packages I will have unit tests using mocha. But as it stands, only one of my packages is ready for unit tests.
From my understanding, shouldn't I be able to only install mocha on the root package.json and then run lerna run test in order to run it only in the packages that have available test scripts in their num packages, correct?
Unfortunately as it stands this is not working.
I have mocha installed as a devDependency in the root. However, if I do run lerna run test --scope=myPackage I get the following message:
mocha: command not found
Because it is in fact installed in the root, shouldn't it work for all packages? If it isn't the case, what am I missing here? DO I have to install mocha in all of them after all?

Just remove the test script from package.json wherever the unit tests are not ready and lerna wont call it.

Related

Handle over a cypress.io testcase to non-technical Users

I've written a testcase with cypress.io which is working fine.
I want that a couple of non-technical people can run this test from their local machines.
My assume was:
Install node-js on the local machines
unzip my project and change to this directory
run ./node_modules/.bin/cypress open
Problem is that the users get the error that cypress is not installed. is there any solution that the users don't have to run npm install cypress --save-dev first? in my understanding i deliver the cypress installation right within my project?
Pls see my project structure here:
on your project you should have a package.json file with cypress in it
"devDependencies": {"cypress": "^4.0.0"}
after they install nodejs, they just need to go into the folder and run npm install or npm i for short.
if you ever encounter an issue with the npm command, use npx instead

Mocha "cannot find module" error

node v6.10.2, npm v3.10.3, mocha v3.1.2
my package.json has this in the scripts section:
"test": "mocha ./test/unit/* --recursive"
When I run npm test I get
> #hc/pos-onlineordering#0.1.0 test C:\_dev\pos-onlineordering\server
> mocha ./test/unit/* --recursive "--reporter" "dot"
module.js:471
throw err;
^
Error: Cannot find module 'C:\_dev\app\server\test\unit\deploy'
it just so happens that C:_dev\app\server\test\unit\deploy is a directory holding several test files in it.
Here's where it gets really fun though. If I enter mocha ./test/unit/* --recursive at the command prompt, it runs all the tests like I would expect. I need it to run as a package script because the running the unit tests are a part of the build and deploy scripts.
More weird: It works for every other team member, some on Macs, some on Windows. I've tried it in Windows Power Shell and in Git-Bash, running as an administrator.
I have deleted the node_modules folder and re-run npm install. I have also run
npm uninstall -D mocha && npm i -D mocha and nothing changes. I have even deleted the entire repository and re-cloned it.
So my assumption is that I clearly have something wrong with my computer or environment, but I don't even know where to look for what it could be at this point. How can I get mocha to run out of the package scripts?

Testing against multiple node module versions

I'm developing several node modules which extend the functionality of the Sequelize database ORM module. I'm using Travis to run tests before pushing new versions of my modules to NPM.
Travis allows you to run tests against different versions of node (v0.10, v0.12 etc). Similarly, I'd like to be able to run my tests using various different versions of the Sequelize module. i.e. run the tests once using Sequelize 2.0.0, again using 2.0.1, 2.0.2 etc.
Travis loads modules from NPM automatically according the the versions set in the package.json file, and so only runs tests using the latest module version available according to what's specified in the package.json of my module (in this case ^2.0.0).
Is there any way to interfere with this process and get Travis to run the tests multiple times using a different Sequelize version each time?
I've searched for advice on this on Stack Overflow and elsewhere but not had any luck. But I assume I can't be the only person who wants to do this...
In the end the solution I came up with is to put the following in the travis.yml file:
before_script:
- 'if [ "$SEQ_VERSION" ]; then npm install sequelize#^$SEQ_VERSION.0.0; fi'
env:
- SEQ_VERSION=2
- SEQ_VERSION=3
(with thanks to Sandro Munda for his answer which put me on the right track)
In your package.json, you could have some something like this:
...
"scripts": {
"test-sequelize-2.0.0": "npm install sequelize#2.0.0 && ./node_modules/mocha/bin/mocha",
"test-sequelize-2.1.0": "npm install sequelize#2.1.0 && ./node_modules/mocha/bin/mocha",
"test": "npm run test-sequelize-2.0.0 && npm run test-sequelize-2.1.0"
}
...
Then, run npm test.
test-all-versions is a package that will run a command (you want your test script) for a whole range of semantic versions. so if you want to run your tests against sequelize you might write a config file like this.
#.tav.yml
name: sequelize
versions: ^2.0.0
command: npm run test
This will run your npm test script against all published versions of sequelize that match ^2.0.0 (ie 2.0.0, 2.0.1, etc)
Then in your travis config run the tav command to test against all versions in the given range.

GitHub repo failing Travis CI build because of "console-browserify"

I have an NPM project that uses Jasmine-Node for running unit tests.
My test suite (npm test) runs fine locally, but when I push my commits to GitHub my Travis CI build fails.
https://travis-ci.org/arthurakay/LintRoller/jobs/7002012
Does anyone know what is happening here? I don't think I need "console-browserify" but for some reason the build believes it's necessary.
For reference, see my ./hooks/pre-commit bash script to see where I call the unit test suite.
It was an issue with JSHint, which was corrected in their own repo shortly after I asked this question.
https://github.com/jshint/jshint/commit/28cfa2e369b31afb6ccfb44ee1f0d7df71170133
In short, JSHint now requires console-browserify but at the moment I ran my build their repo was broken (or in some state that broke my build). Having said that, I probably do need to test against a specific version of each of my dependencies.
you could always install console-browserify to check if that solves the error.
npm install -S console-browserify
Perhaps you have console-browserify installed globally on your local machine

How do you install and run Mocha, the Node.js testing module? Getting "mocha: command not found" after install

I'm having trouble getting Mocha to work as expected, and I'd love to say as documented, but there (appears) to not be much documentation on actually getting the thing running.
I've installed it using npm (both globally and locally), and each time when I run it I get:
$ mocha
mocha: command not found
Ok, so I figured it's not in my PATH, so I tried running it directly,
$ ./node_modules/mocha/bin/mocha
execvp(): No such file or directory
Finally, I tried hitting the other bin file, and got,
$ ./node_modules/mocha/bin/_mocha
path.existsSync is deprecated. It is now called `fs.existsSync`.
.
✔ 1 tests complete (1ms)
How can I just execute my tests with a single command? Vows seems to let you, but I've heard Mocha is the better choice, I just can't seem to get it working correctly.
And any thoughts on the error I got above in my third attempt?
Edit:
I'm running,
Ubuntu 11.10 64-bit
Node.js 0.7.5
npm 1.1.8
mocha 0.14.1
should 0.6.0
since npm 5.2.0, there's a new command "npx" included with npm that makes this much simpler, if you run:
npx mocha <args>
Note: the optional args are forwarded to the command being executed (mocha in this case)
this will automatically pick the executable "mocha" command from your locally installed mocha (always add it as a dev dependency to ensure the correct one is always used by you and everyone else).
Be careful though that if you didn't install mocha, this command will automatically fetch and use latest version, which is great for some tools (like scaffolders for example), but might not be the most recommendable for certain dependencies where you might want to pin to a specific version.
You can read more on npx here
Now, if instead of invoking mocha directly, you want to define a custom npm script, an alias that might invoke other npm binaries...
you don't want your library tests to fail depending on the machine setup (mocha as global, global mocha version, etc), the way to use the local mocha that works cross-platform is:
node node_modules/.bin/mocha
npm puts aliases to all the binaries in your dependencies on that special folder.
Finally, npm will add node_modules/.bin to the PATH automatically when running an npm script, so in your package.json you can do just:
"scripts": {
"test": "mocha"
}
and invoke it with
npm test
After further reading, and confirmation from Linus G Thiel above, I found I simply had to,
Downgrade to Node.js 0.6.12
And either,
Install Mocha as global
Add ./node_modules/.bin to my PATH
To run Mocha with mocha command from your terminal you need to install mocha globally on this machine:
npm install --global mocha
Then cd to your projectFolder/test and run mocha yourTestFileName.js
If you want to make mocha available inside your package.json as a development dependency:
npm install --save-dev mocha
Then add mocha to your scripts inside package.json.
"scripts": {
"test": "mocha"
},
Then run npm test inside your terminal.
While installing the node modules for mocha I had tried the below commands
npm install
npm install mocha
npm install --save-dev mocha
npm install mocha -g # to install it globally also
and on running or executing the mocha test I was trying
mocha test
npm run test
mocha test test\index.test.js
npm test
but I was getting the below error as:
'Mocha' is not recognized as internal or external command
So , after trying everything it came out to be just set the path to environment variables under the System Variables as:
C:\Program Files\nodejs\
and it worked :)
For windows :
Package.json
"scripts": {
"start": "nodemon app.js",
"test": "mocha"
},
then run the command
npm run test
Late answer but I think will work.
Install mocha globally
npm install --global mocha
If you have already installed mocha then set the path to bin

Resources