Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

enter image description here

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.

like image 785
callumacrae Avatar asked Nov 13 '14 11:11

callumacrae


People also ask

Do Mocha tests run sequentially?

According to it, tests are run synchronously. This only shows that ordered code is run in order. That doesn't happen by accident.

How do I run a specific test in Mocha?

Run a Single Test File Using the mocha cli, you can easily specify an exact or wildcarded pattern that you want to run. This is accomplished with the grep option when running the mocha command. The spec must have some describe or it that matches the grep pattern, as in: describe('api', _ => { // ... })

Can Mocha rerun tests?

Retrying Mocha. Mocha. js provides a this. retries() function that allows you specify the number of times a failed test can be retried. For each retry, Mocha reruns the beforeEach() and afterEach() hooks but not the before() and after() hooks.

Do Mocha tests run in parallel?

Mocha does not run individual tests in parallel. If you only have one test file, you'll be penalized for using parallel mode.


3 Answers

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.

like image 73
Louis Avatar answered Sep 30 '22 14:09

Louis


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

like image 45
mantoni Avatar answered Oct 03 '22 14:10

mantoni


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

mocha --grep '^(?!.*\\b\(slow\)\\b)'
like image 30
fmsf Avatar answered Oct 04 '22 14:10

fmsf