最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - With mocha, how do I run all tests that *don't* have (slow) in the name? - Stack Overflow

programmeradmin0浏览0评论

I have a load of tests, and some of them have "(slow)" in the name:

Some of them are slower than the tests marked (slow), but are relied on by other tests and so cannot be skipped. I would just like to skip the ones with (slow) in the name—is that possible?

I'm using Mocha.

I have a load of tests, and some of them have "(slow)" in the name:

Some of them are slower than the tests marked (slow), but are relied on by other tests and so cannot be skipped. I would just like to skip the ones with (slow) in the name—is that possible?

I'm using Mocha.

Share Improve this question asked Nov 13, 2014 at 11:51 callumacraecallumacrae 8,4339 gold badges33 silver badges50 bronze badges 1
  • I would like to point out that if one test relies upon another test, then the dependency should be moved to a before() hook, which will be called for the tests in question. Nesting describe() suites is also beneficial. For multiple tests on the same thing, I tend to put the slow thing in a promise await'ed on by the before hook, and also await'ed on by the tests. And then in the tests, I put the code I expect to actually fail (like assert.deepEqual) – RoboticRenaissance Commented Dec 21, 2020 at 19:12
Add a comment  | 

4 Answers 4

Reset to default 6

It looks to me like you are doing it for a page you are loading in a browser to run Mocha. To do this in the browser you can pass these parameters in the URL of the page:

  • grep which approximately corresponds to the --grep option on the command line. This narrows the tests run to those that match the expression passed to grep. However, there is currently (even as of 2.0.1) no way to get Mocha to interpret this parameter as a regular expression. It is always interpreted as a string. That's why I said "approximately corresponds". --grep on the command line is a regular expression but the grep parameter passed in a URL is a string.

  • invert which correspond to the --invert option on the command line. This will invert the match performed by grep and thus selects the tests that grep does not match.

So if you open you page by appending the following string ?grep=(slow)&invert=1 it will run the tests that do not have the string "(slow)" in them.

You can do this with a combination of two command line switches. Here is the relevant part of the documentation:

-g, --grep <pattern> only run tests matching <pattern> -i, --invert inverts --grep matches

Grep accepts a regex pattern, you can do it like this:

mocha --grep '^(?!.*\\b\(slow\)\\b)'
mocha --opts mocha.opts --grep "^(?!.*SomeExpression)"

I couldn't add parenthesis to the expression - bash / mocha fails. I suggest you to remove the parenthesis and put a tag like @performance in the descriptions and execute mocha with a grep expression like previous one.

发布评论

评论列表(0)

  1. 暂无评论