Run a single unit test with Mocha/Chai

Sure, you can add file globbing patterns to a CLI arg to run a single JavaScript test, or group of tests, but it’s not super convenient and often requires a trip to your README to remember how to do it. Here’s a quicker way.

it.only()

Instead of hunting for the exact CLI params and globs, just add .only  to your it()  as shown below:

describe('api', () => {
  // Run only the following test case.
  it.only('getFooterData() should return a Promise which resolves successfully', () => {
    const promise = api.getFooterData();

    return Promise.all([
      expect(promise).to.eventually.be.fulfilled,
      expect(promise).to.eventually.be.an('object')
    ]);
  });

  it('getHeaderData() should return a Promise which resolves successfully', () => {
    const promise = api.getHeaderData();

    return Promise.all([
      expect(promise).to.eventually.be.fulfilled,
      expect(promise).to.eventually.be.an('object')
    ]);
  });
});

Take note:  If you use .only()  on more than one it() , it will only run the last test case.

describe.only()

And it works with a single group of tests, as well! Just add .only to your describe() as shown below:

// Run only the following group of test cases.
describe.only('api', () => {
  it('getFooterData() should ...', () => {
    // ...
  });

  it('getHeaderData() should ...', () => {
    // ...
  });
});

Wrap up

There you have it! Single test groups and/or cases without screwing with CLI parameters.

Be sure to remove all instances of .only from your describe() and it()  statements before committing to a repo, or you risk having your CI/CD pipeline run only a subset of your tests!