112

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
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Stephen Melrose
  • 4,772
  • 5
  • 29
  • 42
  • 2
    With node 0.6.12, npm 1.1.4, and executing `npm install mocha`, I get `./node_modules/.bin/mocha` as expected. `sudo npm install -g mocha` gets me `/usr/local/bin/mocha`. If this doesn't work properly in your environment, it might be a bug, or just mocha not updated to support node 0.7.*. Regarding your deprecation warning, `exists` and `existsSync` were moved from `path` to `fs` in node 0.7.1. – Linus Thiel Mar 15 '12 at 15:39
  • I downgraded to Node.js 0.6.12, added "./node_modules/.bin" to my PATH, and all is right with the world. – Stephen Melrose Mar 15 '12 at 16:19

6 Answers6

206

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
Benja
  • 4,099
  • 1
  • 30
  • 31
  • 8
    @michael.kebe no, you don't need to specify that path to use the local mocha, npm already adds "./node_modules/.bin" to the $PATH, so it will use the local version by default. – Benja Nov 17 '15 at 12:37
  • I believe that the command should be node node_modules/mocha/bin/mocha not node node_modules/.bin/mocha – Mina Luke Mar 09 '16 at 16:06
  • 3
    @MinaLuke that works too but it's a bad idea, `/node_modules/.bin/{module-binary-name}` is the place where npm puts shortcuts to binaries in your dependencies, you shouldn't reference directly a file in the sources of a dependency, as that might break on the next version. – Benja Mar 09 '16 at 20:53
18

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
Stephen Melrose
  • 4,772
  • 5
  • 29
  • 42
  • 4
    I have node 0.8.12 and mocha both globally and locally installed (for different projects), and they are working. So no need to downgrade node. For locally installed mocha, just make sure your PATH is set as above, or call it directly from your script (Makefile or "scripts" tag). – Qichao Dong Oct 16 '12 at 17:32
  • 1
    I had Mocha installed locally and globally but it wasn't found in the path (Win8). I had to manually add the './node_modules/.bin' folder relative to my project to my PATH, restart console then typing 'mocha' would work. Not ideal, but a quick hack to get it working. Perhaps if you had a top level location to install mocha locally, then link to that PATH so you could use it elsewhere. – Ali Aug 02 '13 at 01:27
5

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.

Qui-Gon Jinn
  • 3,722
  • 3
  • 25
  • 32
2

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 :)

DHEERAJ
  • 571
  • 6
  • 9
1

For windows :

Package.json

  "scripts": {
    "start": "nodemon app.js",
    "test": "mocha"
  },

then run the command

npm run test
Ashutosh Jha
  • 15,451
  • 11
  • 52
  • 85
1

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

harsh 1868
  • 56
  • 6