How to run a single test with Mocha?

前端 未结 12 1163
無奈伤痛
無奈伤痛 2020-12-04 05:46

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?

12条回答
  •  甜味超标
    2020-12-04 05:56

    Try using mocha's --grep option:

        -g, --grep             only run tests matching 
    

    You can use any valid JavaScript regex as . 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.

提交回复
热议问题