Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Mocha tests automatically on each source file change

What is the simplest way to tell Mocha to watch for source project files changes so that it can re-runs its tests?

like image 224
jbarros Avatar asked Nov 25 '18 23:11

jbarros


2 Answers

Run with the watch flag

mocha -w ./tests

And, if your test folder is called just test/ then you don't need to point out the folder (Mocha looks for changes in such folder by default), so you can end up just with:

mocha -w
like image 147
James Avatar answered Nov 07 '22 08:11

James


I definitely do not recommend mocha watch function for the following reasons:

  • The file watcher does not work properly;
  • It is not compatible with ES modules;

To solve this, I did the following changes on my project:

  • Install nodemom: npm install nodemon --save-dev;
  • Install mocha, if not have it: npm install mocha --save-dev;
  • Execute this command in your terminal changing the ./test to point to your test folder: ./node_modules/nodemon/bin/nodemon.js --watch . --exec 'mocha ./test || true';
  • You can also put this command inside the scripts.test section in the file package.json to make you life easy;

References:

  • https://github.com/remy/nodemon/issues/496#issuecomment-325150315
like image 1
Ângelo Polotto Avatar answered Nov 07 '22 10:11

Ângelo Polotto