I have a code where certain tests will always fail in CI environment. I would like to disable them based on an environment condition.
How to programmatically skip a test in mocha during the runtime execution?
I have a code where certain tests will always fail in CI environment. I would like to disable them based on an environment condition.
How to programmatically skip a test in mocha during the runtime execution?
You can skip tests by placing an x in front of the describe or it block, or placing a
.skip
after it.You can also run a single test by placing a
.only
on the test. for instanceOnly the feature 2 block would run in this case.
There doesn't appear to be a way to programmatically skip tests, but you could just do some sort of check in a
beforeEach
statement and only run the test if the flag was set.You can use my package mocha-assume to skip tests programmatically, but only from outside the tests. You use it like this:
Mocha-assume will only run your test when
myAssumption
istrue
, otherwise it will skip it (usingit.skip
) with a nice message.Here's a more detailed example:
Using it this way, you can avoid cascading failures. Say the test
"Does something cool"
would always fail when someAssumption does not hold - But this assumption was already tested above (inTests that verify someAssuption is always true"
).So the test failure does not give you any new information. In fact, it is even a false-positive: The test did not fail because "something cool" did not work, but because a precondition for the test was not satisfied. with
mocha-assume
you can often avoid such false positives.Say I wanted to skip my parametrized test if my test description contained the string "foo", I would do this:
In your case, I believe that if you wanted to check environment variables, you could use NodeJS's:
For example (Warning: I haven't tested this bit of code!), maybe something like this:
Where you can set ENV_VARIABLE to be whatever you are keying off of, and using that value, skip or run the test. (FYI the documentation for the NodeJS' process.env is here: https://nodejs.org/api/process.html#process_process_env)
I won't take complete credit for the first part of this solution, I found and tested the answer and it worked perfectly to skip tests based on a simple condition through this resource: https://github.com/mochajs/mocha/issues/591
Hope this helps! :)
https://mochajs.org/
to skip tests, use
describe.skip
orit.skip
to include tests you could use
describe.only
More info at https://mochajs.org/
This answer does work for ES6.
Instead of:
You want:
This conditionally skips all tests in the describe block IF the condition is false.
Or, instead of:
You want:
This conditionally skips one test IF the condition is false.