How to run a single test with Mocha?

How to run a single test with Mocha?

I use Mocha to test my JavaScript stuff. My test file contains 5 tests. Is that possible to run a specific test (or set of tests) rather than all the tests in the file?


Solution 1:

Try using mocha’s --grep option:

    -g, --grep <pattern>            only run tests matching <pattern>

You can use any valid JavaScript regex as <pattern>. For instance, if we have test/mytest.js:

it('logs a', function(done) {

it('logs b', function(done) {


$ mocha -g 'logs a'

To run a single test. Note that this greps across the names of all describe(name, fn) and it(name, fn) invocations.

Consider using nested describe() calls for namespacing in order to make it easy to locate and select particular sets.

Solution 2:

Depending on your usage pattern, you might just like to use only. We use the TDD style; it looks like this:

test.only('Date part of valid Partition Key', function (done) {

Only this test will run from all the files/suites.

Related:  How to find out which JavaScript events fired?

Solution 3:

If you are using npm test (using package.json scripts) use an extra -- to pass the param through to mocha

e.g. npm test -- --grep "my second test"

EDIT: Looks like --grep can be a little fussy (probably depending on the other arguments). You can:

Modify the package.json:

"test:mocha": "mocha --grep \"<DealsList />\" .",

Or alternatively use --bail which seems to be less fussy

npm test -- --bail

Solution 4:

run single test –by filename–

Actually, one can also run a single mocha test by filename (not just by „it()-string-grepping“) if you remove the glob pattern (e.g. ./test/**/*.spec.js) from your mocha.opts, respectively create a copy, without:

node_modules/.bin/mocha --opts test/mocha.single.opts test/self-test.spec.js

Here’s my mocha.single.opts (it’s only different in missing the aforementioned glob line)

--require ./test/common.js
--compilers js:babel-core/register
--reporter list

Background: While you can override the various switches from the opts-File (starting with --) you can’t override the glob. That link also has
some explanations.

Related:  How do I get the day of the month in javascript?

Hint: if node_modules/.bin/mocha confuses you, to use the local package mocha. You can also write just mocha, if you have it installed globally.

And if you want the comforts of package.json: Still: remove the **/*-ish glob from your mocha.opts, insert them here, for the all-testing, leave them away for the single testing:

"test": "mocha ./test/**/*.spec.js",
"test-watch": "mocha -R list -w ./test/**/*.spec.js",
"test-single": "mocha $1",
"test-single-watch": "mocha -R list -w $1",


> npm run test


> npm run test-single -- test/ES6.self-test.spec.js 

(mind the --!)

Solution 5:

Hi above solutions didn’t work for me.
The other way of running a single test is

mocha test/cartcheckout/checkout.js -g 'Test Name Goes here'

This helps to run a test case from a single file and with specific name.

Solution 6:

npm test <filepath>

eg :

npm test test/api/controllers/test.js

here ‘test/api/controllers/test.js’ is filepath.