Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 640
Stephen Melrose Avatar asked Mar 15 '12 14:03

Stephen Melrose


People also ask

How do you check mocha is installed or not?

Use npx to solve the error "mocha: command not found", e.g. npx mocha or install the package globally by running npm install -g mocha to be able to use the command without the npx prefix. The fastest way to solve the error is to use the npx command.

How do I install mocha?

Install Mocha In the embedded Terminal ( Alt+F12 ) , type one of the following commands: npm install mocha for local installation in your project. npm install -g mocha for global installation. npm install --save-dev mocha to install Mocha as a development dependency.


1 Answers

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 
like image 53
Benja Avatar answered Oct 24 '22 07:10

Benja