How to Filter Tests in MochaJS

In this article I show you how to filter which tests to run in MochaJS.

Step 1. Clone the starter project

Use git to clone my starter repo into a new folder.

git clone https://github.com/mitchallen/autom8able-mochajs-starter.git mocha-filter

cd mocha-filter/

npm install

Run the default tests to make sure the install went okay:

npm test

Step 2. Write the tests

Replace the contents of test/smoke-test.js with this code:

"use strict";

describe('smoke test', function() {
  it('should test this @alpha', function(done) {
    done();
  });
  it('should test that @beta', function(done) {
    done();
  });
  it('should test something else @alpha', function(done) {
    done();
  });
  it('should test another thing', function(done) {
    done();
  });
  context('group @alpha', function() {
    it('should test as part of group', function(done) {
      done();
    });
    it('should test this too as part of group', function(done) {
      done();
    });
  });
});

Step 3. Add test scripts

Edit the scripts section of package.json and add the scripts below.

"scripts": {
  "test": "mocha",
  "test:alpha": "./node_modules/mocha/bin/mocha -g @alpha",
  "test:not:alpha": "./node_modules/mocha/bin/mocha -g @alpha -i",
  "test:beta": "./node_modules/mocha/bin/mocha -g @beta"
},

The default test script

The default test script should run all tests, because it isn’t filtered.

The test:alpha script

The test:alpha script passes the -g (–grep) flag to mocha followed by a label to filter by.

This will cause mocha to only run tests that have the string @alpha in their description. Or tests that are under a context that has the string @alpha.

The test:not:alpha script

The test:not:alpha script passes the -g (–grep) flag to mocha followed by a label to filter by. But it also follows that with the -i (–invert flag). That tells mocha to invert the list of tests to run. In otherwords, do NOT run tests or contexts that contain the label @alpha.

The test:beta script

The test:beta test script works the same as the test:alpha test script. But for tests that contain the label @beta instead of @alpha.

Step 4. Run the tests unfiltered

Run the default test script that has no filter.

npm test

All the tests should run:

  smoke test
    ✓ should test this @alpha
    ✓ should test that @beta
    ✓ should test something else @alpha
    ✓ should test another thing
    group @alpha
      ✓ should test as part of group
      ✓ should test this too as part of group

  6 passing (7ms)

Step 5. Run the tests filtered by a label

Run the tests using the script that filters by a string (label) using the grep (-g) flag:

npm run test:alpha

Only tests with the string @alpha, or tests grouped under a context with that label should run:

> ./node_modules/mocha/bin/mocha -g @alpha

  smoke test
    ✓ should test this @alpha
    ✓ should test something else @alpha
    group @alpha
      ✓ should test as part of group
      ✓ should test this too as part of group

  4 passing (6ms)

Step 6. Run the tests skipped by a label

Run the tests using the script that filters by a string (label) using the grep (-g) flag and the invert (-i) flag:

npm run test:not:alpha

Tests with the string @alpha, or tests grouped under a context with that label should not run. But all other tests should run:

> ./node_modules/mocha/bin/mocha -g @alpha -i

  smoke test
    ✓ should test that @beta
    ✓ should test another thing

  2 passing (5ms)

Troubleshooting

If a test you think should run, doesn’t run, check the spelling of the label or filter.

Conclusion

In this article you learned how to:

  • Use the mocha -g flag to filter running tests and contexts by label
  • Use the mocha -i flag to skip running tests and contexts by label
  • Create new npm scripts and run them

References

  • mochajs.org [1]



About the Author

Mitch Allen tests cloud services for a robotics company in New England.