Run mocha excluding paths

2020-05-25 06:18发布

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?

标签: npm mocha
7条回答
爷的心禁止访问
2楼-- · 2020-05-25 06:34

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.
查看更多
淡お忘
3楼-- · 2020-05-25 06:36

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 and (spec key)
  • excludes the example (or can be any experimantal 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.

查看更多
家丑人穷心不美
4楼-- · 2020-05-25 06:43

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.

查看更多
The star\"
5楼-- · 2020-05-25 06:47

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.

查看更多
祖国的老花朵
6楼-- · 2020-05-25 06:49

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.

查看更多
\"骚年 ilove
7楼-- · 2020-05-25 06:57

For Windows users This script will run perfectly

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

I hope this will help.

cheers!

查看更多
登录 后发表回答