Running a single Mocha Test

If you want to run a single mocha spec, you just specify the individual file to the mocha command:

mocha specs/ralphsays-spec.js 

But what if you want to run a single test within that file?

What I initially did was comment out the other tests temporarily, but this isn’t efficient or foolproof. I found there’s an .only() method for a test, so you simply use this on the only test you want to run.'shows a quote container', function() {
    var ralphSaysPage = new RalphSaysPage(driver, true);
    ralphSaysPage.quoteContainerPresent().then(function(present) {
        assert.equal(present, true, "Quote container not displayed");

This means you run the same mocha command but it only runs the single test you marked. Neat.

Author: Alister Scott

Alister is an Excellence Wrangler for Automattic.

3 thoughts on “Running a single Mocha Test”

  1. If your test has unique test ids or pattern in the title, you can use the mocha -g option to specify which test case to run in command line.


Comments are closed.