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?
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
});
});`
Most helpful comment
The different for dependency injection is:
and
I find the latter quite annoying – manually managing scope.