This inclusive ability is available in Mocha by appending . skip() to the suite or to specific test cases. The skipped tests will be marked as "pending" in the test results.
As of the mocha documentation, you can add --exit flag when you are executing the tests. It will stop the execution whenever all the tests have been executed successfully or not.
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', _ => { // ... })
You can skip tests by placing an x in front of the describe or it block, or placing a .skip
after it.
xit('should work', function (done) {});
describe.skip('features', function() {});
You can also run a single test by placing a .only
on the test. for instance
describe('feature 1', function() {});
describe.only('feature 2', function() {});
describe('feature 3', function() {});
Only 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.
beforeEach(function(){
if (wrongEnvironment){
runTest = false
}
}
describe('feature', function(){
if(runTest){
it('should work', function(){
// Test would not run or show up if runTest was false,
}
}
}
skip()
functionIt can be used to either statically to disable a test or entire suite, or dynamically skip it at runtime.
Here's an example runtime usage:
it('should only test in the correct environment', function() {
if (/* check test environment */) {
// make assertions
} else {
this.skip();
}
});
This answer does work for ES6.
Instead of:
describe('your describe block', () => {
You want:
(condition ? describe : describe.skip)('your describe block', () => {
This conditionally skips all tests in the describe block IF the condition is false.
Or, instead of:
it('your it block', () => {
You want:
(condition ? it : it.skip)('your it block', () => {
This conditionally skips one test IF the condition is false.
I use runtime skipping from Mocha for the same scenario as you're describing. It is the copy paste from the docs:
it('should only test in the correct environment', function() {
if (/* check test environment */) return this.skip();
// make assertions
});
As you can see, it skips the test based on environment. My own condition is if(process.env.NODE_ENV === 'continuous-integration')
.
describe.skip
or it.skip
describe('Array', function() {
it.skip('#indexOf', function() {
// ...
});
});
describe.only
or it.only
describe('Array', function() {
it.only('#indexOf', function() {
// ...
});
});
More info at https://mochajs.org/#inclusive-tests
It depends how you want to programmatically skip the test. If the conditions for skipping can be determined before any test code is run, then you can just call it
or it.skip
as needed, based on a condition. For instance, this will skip some tests if the environment variable ONE
is set to any value:
var conditions = {
"condition one": process.env["ONE"] !== undefined
// There could be more conditions in this table...
};
describe("conditions that can be determined ahead of time", function () {
function skip_if(condition, name, callback) {
var fn = conditions[condition] ? it.skip: it;
fn(name, callback);
};
skip_if("condition one", "test one", function () {
throw new Error("skipped!");
});
// async.
skip_if("condition one", "test one (async)", function (done) {
throw new Error("skipped!");
});
skip_if("condition two", "test two", function () {
console.log("test two!");
});
});
If the conditions you want to check can only be determined at test time, it is a bit more complicated. If you do not want to access anything not strictly speaking part of the testing API, then you could do this:
describe("conditions that can be determined at test time", function () {
var conditions = {};
function skip_if(condition, name, callback) {
if (callback.length) {
it(name, function (done) {
if (conditions[condition])
done();
else
callback(done);
});
}
else {
it(name, function () {
if (conditions[condition])
return;
callback();
});
}
};
before(function () {
conditions["condition one"] = true;
});
skip_if("condition one", "test one", function () {
throw new Error("skipped!");
});
// async.
skip_if("condition one", "test one (async)", function (done) {
throw new Error("skipped!");
});
skip_if("condition two", "test two", function () {
console.log("test two!");
});
});
Whereas my first example was marking the tests as formally skipped (aka "pending"), the method I've just shown will just avoid performing the actual test but the tests won't be marked as formally skipped. They will be marked as passed. If you absolutely want to have them skipped I don't know of any way short of accessing parts that are not properly speaking part of the testing API:
describe("conditions that can be determined at test time", function () {
var condition_to_test = {}; // A map from condition names to tests.
function skip_if(condition, name, callback) {
var test = it(name, callback);
if (!condition_to_test[condition])
condition_to_test[condition] = [];
condition_to_test[condition].push(test);
};
before(function () {
condition_to_test["condition one"].forEach(function (test) {
test.pending = true; // Skip the test by marking it pending!
});
});
skip_if("condition one", "test one", function () {
throw new Error("skipped!");
});
// async.
skip_if("condition one", "test one (async)", function (done) {
throw new Error("skipped!");
});
skip_if("condition two", "test two", function () {
console.log("test two!");
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With