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?
Learn how to run only one test with Mocha To run a single test (as defined in it() ), you can simply call the only() method on it in the following way: it. only('...', function () { // only this test will run... });
So to run a single test, there are two approaches: Option 1: If your test name is unique, you can enter t while in watch mode and enter the name of the test you'd like to run. Option 2: Hit p while in watch mode to enter a regex for the filename you'd like to run.
In order to run a specific test, you'll need to use the jest command. npm test will not work. To access jest directly on the command line, install it via npm i -g jest-cli or yarn global add jest-cli . Then simply run your specific test with jest bar.
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) { console.log('a'); done(); }); it('logs b', function(done) { console.log('b'); done(); });
Then:
$ 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With