Skip to content

Instantly share code, notes, and snippets.

@timoxley
Created February 2, 2012 04:58
Show Gist options
  • Save timoxley/1721593 to your computer and use it in GitHub Desktop.
Save timoxley/1721593 to your computer and use it in GitHub Desktop.
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"
},
@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

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

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

Thank you very much!

@gaurav51289
Copy link

it works for me to wrap glob path into quotes

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

๐Ÿ––

@margonzalez
Copy link

it works for me to wrap glob path into quotes

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

Thanks, it worked! ๐Ÿ‘Œ

@alitarlaaci1981
Copy link

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

@azazrehman
Copy link

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.
[email protected]

@faethonm
Copy link

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.

@ANUR46
Copy link

ANUR46 commented Sep 21, 2023

for me its working using --recursive

"scripts": {
    "test": "mocha --recursive --exit"
}

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