且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Mocha运行单个测试?

更新时间:2023-01-11 17:07:48

尝试使用 mocha的 - grep 选项

Try using mocha's --grep option:

    -g, --grep <pattern>            only run tests matching <pattern>

您可以将任何有效的JavaScript正则表达式用作< pattern> 。例如,如果我们有 test / mytest.js

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

然后:

$ mocha -g 'logs a'

运行单个测试。请注意,这会覆盖所有 describe(name,fn) it(name,fn)调用的名称。

To run a single test. Note that this greps across the names of all describe(name, fn) and it(name, fn) invocations.

考虑使用嵌套的 describe()调用命名空间,以便于查找和选择特定集合。

Consider using nested describe() calls for namespacing in order to make it easy to locate and select particular sets.