conditional describe

494 views
Skip to first unread message

Oleg Korobenko

unread,
Sep 30, 2014, 10:27:42 AM9/30/14
to moc...@googlegroups.com
Is it possible to add describe only if some condition (which depends on dynamic data - callculated inside preceding it) is true, e.g.

var isSecond = false;
describe('1', function() {
    it('1', function() {
        isSecond = true;
    });
});

if (isSecond) describe('2', function() {
    it('2', function() {});
});

describe 2 will not be executed.

Is it possible to achieve such behavior when describe 2 will be executed in another way?

Miroslav Bajtoš

unread,
Oct 1, 2014, 7:13:14 AM10/1/14
to moc...@googlegroups.com
On Tuesday, September 30, 2014 4:27:42 PM UTC+2, Oleg Korobenko wrote:
Is it possible to add describe only if some condition (which depends on dynamic data - callculated inside preceding it) is true, e.g.

AFAIK, that is not possible. All `describe` blocks are run immediately at the start to build a list of tests and suites.

However, it is possible to write a custom `it` that will skip tests when the condition is false.

function whenSecondIt(name, fn) {
  it(name, function() {
    if (isSecond) fn();
  });
}

describe('2', function() {
  whenSecondIt('2', function() {});
});

Unfortunately there is no way how to mark the test as skipped from inside the test function (at least AFAIK), therefore the tests will be marked as PASSed by mocha.

Miroslav

Vlad GURDIGA

unread,
Oct 2, 2014, 4:57:26 AM10/2/14
to moc...@googlegroups.com
I think this other thread may have a potential solution for your question:

Oleg Korobenko

unread,
Oct 27, 2014, 10:25:45 AM10/27/14
to moc...@googlegroups.com
Yes, i know it - we can use results of async calls inside `it` but not `describe`, but thanks for the confirmation of my suspicions

btw, here is https://github.com/mochajs/mocha/issues/332 discussion about skipping tests at runtime

Oleg Korobenko

unread,
Oct 27, 2014, 10:38:43 AM10/27/14
to moc...@googlegroups.com
Vlad, there is no solution for me because i need to use result of async call to determine whether to execute `describe` or not, but thanks anyway

Vlad GURDIGA

unread,
Oct 27, 2014, 10:58:54 AM10/27/14
to moc...@googlegroups.com
Oh, If I’d need to do that I would probably use a flag. Something like this:

describe('The feature', function() {
  var isRelevantToContext;

  beforeEach(function(done) {
    asynclyCheckIfRelevantToContext()
    .then(function(isRelevant) {
      isRelevantToContext = isRelevant;
    })
    .then(done, done);
  });

  it('fulfills the requirements', function() {
    if (!isRelevantToContext) return; // this will make the test “succeed”

    // ... assertions ...
  });
});

This is not perfect, but I think I’d go for it.

Олег Коробенко

unread,
Oct 27, 2014, 11:05:42 AM10/27/14
to moc...@googlegroups.com
I'm using it in this way, but this is really annoying when you have `describe` with 10 `it` and you should start each `it` with `if (!isRelevantToContext`

--
You received this message because you are subscribed to a topic in the Google Groups "Mocha" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mochajs/HL9QyujUXag/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mochajs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Олег Коробенко

unread,
Oct 27, 2014, 11:08:47 AM10/27/14
to moc...@googlegroups.com
It is also bad that mocha think that those tests are passed, so you should add something like 'is relevant to context' to the start/end of your `it` strings
Reply all
Reply to author
Forward
0 new messages