When working inside your test suite, it’s often the case that you don’t want to run all of your tests. Mocha has a great mechanism that allows you to easily specify what exactly you want run. This can help make your processes such as TDD faster and more focused.
Run a Single Test File
Using the mocha cli, you can easily specify an exact or wildcarded pattern that you want to run. This is accomplished with the
grep option when running the
If you wanted to run only the login-failure.js suite, the command would look like:
If you happen to have multiple suites that match a filename pattern that you wanted to run, exclusive to all others, you would run:
Run a Single Spec
Often we’re in the test code making changes, and it’s easy to make adjustments to what subset of tests you run from here as well. If I want to run a single
describe() function, I can add a
.only() to the function call, like this:
1 2 3 4 5 6
You can stick the
.only() on any
describe, no matter if it’s first or last in the list of specs.
Run a Single Test
Just as you can with
describe, the tests can be specified to exclusively run if you attach
.only() to the function call.
1 2 3 4 5 6
Update 12 Oct 2015: The following note now does not apply.
before, etc hooks are now verified to be run when running a single test.
Note! that by putting the .only() on a test, all other functions in the spec are skipped. This means that your before, beforeEach, after, and afterEach functions are not called. Sometimes it’s hard to remember when that matters, so I usually end up running single specs instead of single tests.
If you want to run all the specs/tests that you have minus some subset, effectively commenting out these tests, you can add
.skip() to either
it function calls.
Remember to Remove
With the additions of the
.skip() you are altering the actual code of your tests. This is great for your local development process. You’ll need to be careful that you don’t commit this change – at least you usually won’t want to. Otherwise, you CI process will be running a subset of your tests, just as you are locally, which is no bueno.