Run mocha excluding paths

For Windows users This script will run perfectly

 "test": "mocha \"./{,!(node_modules)/**/}*.test.js\"",

I hope this will help.

cheers!


You can exclude files in mocha by passing opts

mocha -h|grep -i exclude
    --exclude <file>                        a file or glob pattern to ignore (default: )

mocha --exclude **/*-.jest.js

Additionally, you can also create a test/mocha.opts file and add it there

# test/mocha.opts
--exclude **/*-test.jest.js
--require ./test/setup.js

If you want to exclude a particular file type you could do something like this

// test/setup.js
require.extensions['.graphql'] = function() {
  return null
}

This is useful when processing extensions with a module loader such as webpack that mocha does not understand.


I was able to solve this using globbing patterns in the argument to mocha. Like you I didn't want to put all my tests under a single tests folder. I wanted them in the same directory as the class they were testing. My file structure looked like this:

project
|- lib
   |- class1.js
   |- class1.test.js
|- node_modules
   |- lots of stuff...

Running this from the project folder worked for me:

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

Which match any *.test.js file in the tree, so long is its path isn't rooted at ./node_modules/.

This is an online tool for testing glob patterns that I found useful.

Tags:

Npm

Mocha.Js