Skip to content

Test Globs

By default, mocha looks for the glob "./test/*.{js,cjs,mjs}", so you may want to put your tests in test/ folder. If you want to include subdirectories, pass the --recursive option.

To configure where mocha looks for tests, you may pass your own glob:

Terminal window
$ mocha --recursive "./spec/*.js"

Some shells support recursive matching by using the globstar (**) wildcard. Bash >= 4.3 supports this with the globstar option which must be enabled to get the same results as passing the --recursive option (ZSH and Fish support this by default). With recursive matching enabled, the following is the same as passing --recursive:

Terminal window
$ mocha "./spec/**/*.js"

You should always quote your globs in npm scripts. If you use quotes, the glob module will handle its expansion. For maximum compatibility, surround the entire expression with double quotes and refrain from $, ", ^, and \ within your expression.

See this tutorial on using globs.

Note: Double quotes around the glob are recommended for portability.