Skip to content

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"
},
@jerzygangi
Copy link

jerzygangi commented Nov 21, 2015

Thanks whroman

@cadesalaberry
Copy link

cadesalaberry commented Mar 1, 2016

Thanks whroman, precisely what I was looking for.

@szkrd
Copy link

szkrd commented Jul 18, 2016

A cross platform kitten died.

@jadeydi
Copy link

jadeydi commented Nov 22, 2016

@whroman thks

@mukeshkamboj
Copy link

mukeshkamboj commented Feb 13, 2017

thansk @whroman

@lxjwlt
Copy link

lxjwlt commented Aug 10, 2017

it works for me to wrap glob path into quotes

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

@AustinFelipe
Copy link

AustinFelipe commented Sep 22, 2017

@lxjwlt thanks mate, it works perfectly

@Byloor
Copy link

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
Copy link

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
Copy link

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
Copy link

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'"
  }

@ademidun
Copy link

ademidun commented May 26, 2019

THanks so much for this @timoxley

@Heeseok The following worked for me

"scripts": {
    "test": "find ./**/tests -name '*spec.js' | xargs ./node_modules/.bin/mocha -R spec"
}

@aminnairi
Copy link

aminnairi commented Jul 9, 2019

Also, to avoid using a pipe, you can use this version of find:

"scripts": {
  "test": "find src -name '*.spec.js' -exec mocha --require @babel/register {} \\;"
}

Where:

  • src is the folder where to find your unit test scripts.
  • *.spec.js is the regular expression that will match the files you want to use with mocha.
  • @babel/register a register to use (for instance for TypeScript: --require ts-node/register).

@ademidun
Copy link

ademidun commented Jul 10, 2019

In case anyone is interested, I actually switched to jest, because it seems to have a much easier test script.

"scripts": {
  "test": "jest"
}

That's it 😁

@thanhdat21293
Copy link

thanhdat21293 commented Nov 12, 2019

Thank you very much!

@gaurav51289
Copy link

gaurav51289 commented Jul 15, 2020

it works for me to wrap glob path into quotes

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

🖖

@margonzalez
Copy link

margonzalez commented Nov 3, 2020

it works for me to wrap glob path into quotes

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

Thanks, it worked! 👌

@alitarlaaci1981
Copy link

alitarlaaci1981 commented Nov 9, 2020

anyone among you does web automation with mocha framework with java script?I cant find enough source online.I need help!

@azazrehman
Copy link

azazrehman commented Dec 3, 2020

anyone among you do web automation with a mocha framework with javascript?I can't find enough sources online. I need help!

@alitarlaaci1981 you can connect me in this regard, I use mocha with JS.
azaz.bscs3173@iiu.edu.pk

@faethonm
Copy link

faethonm commented Mar 13, 2021

it works for me to wrap glob path into quotes

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

👍

@vcostin
Copy link

vcostin commented May 31, 2021

it works for me to wrap glob path into quotes

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

🖖

OMG, Thank you!
Without escaped quotes, it behaves totally different, and I thought that I have issues with NYC. I could not generate a proper code coverage report, and it derived me nearly insane.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment