Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a single test with Mocha?

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?

like image 432
Misha Moroshko Avatar asked May 31 '12 10:05

Misha Moroshko


People also ask

How do I run a single test file in Mocha?

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... });

How do I run a specific jest test?

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.

How do I run a single test file in npm?

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.


2 Answers

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.

like image 168
Asherah Avatar answered Oct 24 '22 14:10

Asherah


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.

like image 41
J Burnett Avatar answered Oct 24 '22 14:10

J Burnett