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 the node_modules folder , from the mocha command, by running npm test (in the .json package):

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

and it does not work.

I am using windows.

Any suggestion?

+8
npm mocha
source share
6 answers

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

 . ├── src ├── fileA.js ├── fileA_test.js ├── fileB.js ├── fileB_test.js ├── node_modules ├── ... 

I had to update the path in 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", 

A simple change and it works.

  • I am free to choose any naming conventions that I like.
  • Each test file remains close to the corresponding JS file.
+1
source share

I was able to solve this problem using templates in mocha argumentation. Like you, I did not want to put all my tests in one folder for tests . I wanted them to be in the same directory as the class they tested. 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' 

*.test.js to the *.test.js file in the tree, so its path does not have ./node_modules/ in ./node_modules/ .

This is an online tool for testing globe templates that I found useful.

+10
source share

I am not a mocha guru or ant-style guru, but perhaps it is not possible to exclude a specific path on the mocha command line.

You can put all the test files in the test folder and install your .json package as follows:

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

You can also provide more than one start folder:

 "scripts": { "test": "mocha ./test/**/*_test.js ./another_test_folder/**/*_test.js" } 
+5
source share

For Windows users This script will work just fine

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

I hope this helps.

Hooray!

+2
source share

I had a spec directory containing all my specifications. Inside this directory, I had several subdirectories, one of which was the e2e specs directory. In this scenario, I used the mocha specs $(find specs -name '*.js' -not -path "specs/e2e/*") to run all my tests, ignoring them in the e2e directory.

+1
source share

You can exclude files in mocha by passing options

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

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

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

If you want to exclude a specific file type, you can do something like this

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

This is useful when handling extensions with a module loader such as a web package that mocha does not understand.

0
source share

All Articles