Instantly share code, notes, and snippets.

Embed
What would you like to do?
Recursively run all tests in test directory using mocha
// this will find all files ending with _test.js and run them with Mocha. Put this in your package.json
"scripts": {
"test": "find ./tests -name '*_test.js' | xargs mocha -R spec"
},
@favio41

This comment has been minimized.

favio41 commented Nov 19, 2013

By default mocha will find all .js files, this only reduce the list to _test.js files

@samacs

This comment has been minimized.

samacs commented Dec 11, 2013

It actually find the files that match the pattern in subfolders too. It works for me.

@hugeen

This comment has been minimized.

hugeen commented May 20, 2014

Thank you for this trick

@whroman

This comment has been minimized.

whroman commented Feb 23, 2015

Recursively running all test files in a directory with Mocha can be achieved with a packaged flag.

mocha --recursive

http://mochajs.org/#usage

@dewald-galjaard

This comment has been minimized.

dewald-galjaard commented Mar 26, 2015

whroman +1

@jerzygangi

This comment has been minimized.

jerzygangi commented Nov 21, 2015

Thanks whroman

@cadesalaberry

This comment has been minimized.

cadesalaberry commented Mar 1, 2016

Thanks whroman, precisely what I was looking for.

@szkrd

This comment has been minimized.

szkrd commented Jul 18, 2016

A cross platform kitten died.

@jadeydi

This comment has been minimized.

jadeydi commented Nov 22, 2016

@whroman thks

@mukeshkamboj

This comment has been minimized.

mukeshkamboj commented Feb 13, 2017

thansk @whroman

@lxjwlt

This comment has been minimized.

lxjwlt commented Aug 10, 2017

it works for me to wrap glob path into quotes

"scripts": {
    "test": "mocha \"test/**/*.spec.js\""
}
@AustinFelipe

This comment has been minimized.

AustinFelipe commented Sep 22, 2017

@lxjwlt thanks mate, it works perfectly

@Byloor

This comment has been minimized.

Byloor commented Oct 9, 2017

Does this work in windows? isnt find command for linux ?
I have test:unix : ""test:": "NODE_ENV=testing ./node_modules/mocha/bin/mocha $(find ./test -path '.tests') --reporter spec", -- which works for linux,

I have "test:win": "set NODE_ENV=testing&& node node_modules/mocha/bin/mocha **dont know what to replace it with here ** --reporter spec",

-- Answering my own concern
replacing $(find ./test -path '.tests') with mocha ./test --recursive -- works in Windows

@JavaTheNutt

This comment has been minimized.

JavaTheNutt commented Nov 1, 2017

@Byloor, Near as I can tell, your solution seems to work on both Windows and *NIX systems. This is the form I used:

"unit": "cross-env NODE_ENV=test LOG_LEVEL=silent nyc mocha ./test/unit -name '*_test.js' --recursive --compilers js:babel-core/register -R spec"
@joshperry

This comment has been minimized.

joshperry commented Jan 12, 2018

@lxjwlt, Gracias! This was the simple fix for me. Single quotes also allow you to avoid escaping.

  "scripts": {
    "test": "mocha --require test-setup.js './**/*.spec.js'"
  }
@Heeseok

This comment has been minimized.

Heeseok commented Nov 18, 2018

Single quotes are not working for me in Windows 10.

mocha './**/*.spec.js'

Warning: Could not find any test files matching pattern: './**/*.spec.js'
No test files found
npm ERR! Test failed. See above for more details.

@lxjwlt, Gracias! This was the simple fix for me. Single quotes also allow you to avoid escaping.

  "scripts": {
    "test": "mocha --require test-setup.js './**/*.spec.js'"
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment