Mocha: async describe

Created on 13 Nov 2014  Â·  4Comments  Â·  Source: mochajs/mocha

it, beforeEach, and afterEach allow you to return a promise if you need to do an asynchronous operation.

I've found myself needing similar behaviour for describe:

describe('test', () => {
    return doSomethingAsync().then(() => {
        it('should', () => {
            // test
        });
    });
});

I can only define my tests once I have done something asynchronous. In my case, the async operation I need to do is to inject with my dependency injection framework (https://github.com/angular/di.js). Perhaps there are other use cases?

needs-feedback

Most helpful comment

The different for dependency injection is:

describe('test', () => {
    return doSomethingAsync().then((foo) => {
        it('should', () => {
            // foo is in scope
        });
    });
});

and

describe('test', () => {
    var foo;

    beforeEach(() =>
        doSomethingAsync().then((x) => {
            foo = x;
        }))

    it('should', () => {
        // foo is in scope
    });
});

I find the latter quite annoying – manually managing scope.

All 4 comments

Why is this not possible with before()?

ya i'd just use before or do the async stuff myself.

The different for dependency injection is:

describe('test', () => {
    return doSomethingAsync().then((foo) => {
        it('should', () => {
            // foo is in scope
        });
    });
});

and

describe('test', () => {
    var foo;

    beforeEach(() =>
        doSomethingAsync().then((x) => {
            foo = x;
        }))

    it('should', () => {
        // foo is in scope
    });
});

I find the latter quite annoying – manually managing scope.

What if I need foo to be in scope both inside the mocha test and outside the mocha test. Example:
`describe('test', () => {
var foo;

beforeEach(() =>
    doSomethingAsync().then((x) => {
        foo = x;
    }))

//need foo in scope here as well
it('should', () => {
// foo is in scope
});
});`

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Almad picture Almad  Â·  41Comments

haraldrudell picture haraldrudell  Â·  52Comments

teckays picture teckays  Â·  84Comments

domenic picture domenic  Â·  43Comments

ibc picture ibc  Â·  59Comments