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?
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.
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:
and in package.json:
Simple change and it works.
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
thatspec
key)exclude
key).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.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 singletests
folder. I wanted them in the same directory as the class they were testing. My file structure looked like this:Running this from the
project
folder worked for me: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.
I had a
spec
directory containing all my specs. Within that directory, I had several sub-directories, one of which was thee2e
specs directory. In that scenario, I used themocha specs $(find specs -name '*.js' -not -path "specs/e2e/*")
command to run all my tests ignoring those within thee2e
directory.You can exclude files in mocha by passing opts
Additionally, you can also create a
test/mocha.opts
file and add it thereIf you want to exclude a particular file type you could do something like this
This is useful when processing extensions with a module loader such as webpack that mocha does not understand.
For Windows users This script will run perfectly
I hope this will help.
cheers!