Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha, how to ignore node_modules folder

I am trying to create a test environment which all source file is side by side with its test code. This is due to easy to track which file is without its test code. Below is the example of my directory after run build

built/api/a.js
built/api/a-test.js  
built/api/b.js
built/api/b-test.js
built/index.js
built/index-test.js
built/node_modules/...
built/package.json  
src/api/a.js
src/api/a-test.js
src/api/b.js
src/api/b-test.js
src/index.js
src/index-test.js
src/package.json
package.json

I am going to run my test by run 'npm test', below is my package.json content:

{ "name": "sample", 
  "scripts": {
    "build": "babel ./src -d ./built && cd built && npm install",
    "test": "mocha built/**/*-test.js"
  },
  "devDependencies": {
    "babel-cli": "^6.18.0",
    "babel-core": "^6.18.0",
    "mocha": "^3.1.2"
  }
}

My question is how can I skip those files in node_modules folder coincidently have name end with -test.js

like image 221
Simon Avatar asked Nov 22 '16 06:11

Simon


3 Answers

A little late to the party (13+ months!?), but...

Mocha doesn't support this out of the box. You need to use your glob-fu and get a little fancy. Running something like this on your command line should do the trick:

mocha './built/{,!(node_modules)/**}/*-test.js'

The glob pattern {,!(node_modules)/**} says

Take entries in the current directory, or in any subdirectory of the current directory, regardless of depth, except for ones rooted in ./build/node_modules.

Note that the single quotes are essentially required on OS X or Linux boxen. Left as a bareword (unquoted), or quoted with double quotes, you'll get shell globbing instead of the internal glob() used by mocha, so you're unlikely to get the results you might expect... unless you have an atypical shell.

The Windows shell only supports double quoting... and it doesn't have command line globbing, so on Windows, you'll probably want to leave this unquoted (but don't, err, quote me on that).

A better, more platform-agnostic way would be to put the glob pattern in your test/mocha.opts file, thus:

--require ./test/setup.js
--colors
--inline-diffs
./built/{,!(node_modules)/**}/*-test.js

Cheers!

like image 138
Nicholas Carey Avatar answered Oct 17 '22 06:10

Nicholas Carey


mocha '**/*-test.js' --ignore 'built/node_modules/**/*'
like image 34
Bryan Grace Avatar answered Oct 17 '22 05:10

Bryan Grace


Another alternative to the existing answers... for those who use a mocha config file (.mocharc.js), you can include the ignore parameter:

{
  ignore: 'built/node_modules/**/*'
}

Calling this with mocha --config .mocharc.js built/**/*-test.js

like image 32
Rob C Avatar answered Oct 17 '22 05:10

Rob C