'Run mocha excluding paths

I have this (in gulpfile.js):

var gulp = require("gulp");
var mocha = require("gulp-mocha");
gulp.task("test", function() {
    gulp
        .src(["./**/*_test.js", "!./node_modules/**/*.js"]);
});

and it works.

I want to replicate the same behavior, excluding "node_modules" folder, from mocha command, running npm test (in package.json):

"scripts": {
    "test": "mocha **\\*_test.js !./node_modules/**/*.js*",
}

and it doesn't work.

I'm using Windows.

Any suggestion?



Solution 1:[1]

As suggested in a comment by @thebearingedge, in the end I put ALL the source files (with the relative test files) in a new "src" dir.
In this way I can define the root for tests with a path that exclude by default the "node_modules" folder.

.
??? src  
    ??? fileA.js  
    ??? fileA_test.js  
    ??? fileB.js  
    ??? fileB_test.js  
??? node_modules
    ??? ...

I had to update the path in the package.json, gulpfile.js and in some batch files that I use as utilities.

Changes in gulpfile.js:

.src(["./src/**/*_test.js"]);

and in package.json:

"test": "mocha src\\**\\*_test.js",

Simple change and it works.

  • I'm free to choose whatever naming conventions I like.
  • Each test files remain close to the relative JS file.

Solution 2:[2]

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.

Solution 3:[3]

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.

Solution 4:[4]

For Windows users This script will run perfectly

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

I hope this will help.

cheers!

Solution 5:[5]

I'm not a guru on mocha or ant-style pattern but maybe it isn't possible escluding specific path in the mocha command line.

You can put all your test files under a test folder, and set your package.json like this:

"scripts": {
    "test": "mocha ./test/**/*_test.js"
}

You can also provide more than one starting folder:

"scripts": {
    "test": "mocha ./test/**/*_test.js ./another_test_folder/**/*_test.js"
}

Solution 6:[6]

As of 2019 the modern way of configuring Mocha under Node is via config file in your project root (e.g. via .mocharc.js).

Here is the example of the .mocharc.js that

  • rederfines the default test directory (spec key) and
  • excludes the example (or can be any experimental tests) from the overall suite (exclude key).
module.exports = {
    'spec': 'src/front/js/tests/**/*.spec.js',
    'exclude': 'src/front/js/tests/examples/*.spec.js',
    'reporter': 'dot'
};

As you may see there can be more options used in the config. In part they are just replicas of Mocha CLI options. Just look up ones what you like and try to use within .mocharc.js (use camelCase for dash-comprising CLI options). Or see the config examples.

Solution 7:[7]

I had a spec directory containing all my specs. Within that directory, I had several sub-directories, one of which was the e2e specs directory. In that scenario, I used the mocha specs $(find specs -name '*.js' -not -path "specs/e2e/*") command to run all my tests ignoring those within the e2e directory.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Nicholas Carey
Solution 3 lfender6445
Solution 4 Manvender Singh Rathore
Solution 5 baudo2048
Solution 6
Solution 7