Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run mocha excluding paths

Tags:

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?

like image 359
Alex 75 Avatar asked Dec 15 '15 23:12

Alex 75


People also ask

How do you skip the mocha test?

This inclusive ability is available in Mocha by appending . skip() to the suite or to specific test cases. The skipped tests will be marked as "pending" in the test results.

How do I run a single test file in mocha?

If you just want to run one test from your entire list of test cases then, you can write only ahead of your test case. If you want to run all the test cases which are inside one describe section, then you can also write only to describe as well. describe.

Does mocha run tests in parallel?

Mocha does not run individual tests in parallel. That means if you hand Mocha a single, lonely test file, it will spawn a single worker process, and that worker process will run the file. If you only have one test file, you'll be penalized for using parallel mode. Don't do that.

Do mocha tests run in order?

Mocha will run the tests in the order the describe calls execute.


2 Answers

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.

like image 55
d512 Avatar answered Oct 05 '22 23:10

d512


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.

like image 39
lfender6445 Avatar answered Oct 06 '22 01:10

lfender6445