Why should we not just replace beforeAll(function(){ let foo = 'test' })
with let foo = 'test'
? What's the purpose of beforeAll
if the second way is fine?
Here's the official definition by the way: The beforeAll function executes once and only once for the describe block containing it, before any of the beforeEach functions or any of the specs.
There are some substantial differences between beforeAll
block and plain code in the scope of describe
function.
beforeAll
setup is supposed to be coupled with afterAll
teardown.
Errors in before*
blocks are not critical and won't prevent the list of specs from being populated and specs from being run. An error in describe
block will result in empty list of specs.
before*
and it
blocks have this
as the context of current spec which can be used as recommended way to share data between blocks. It is generally more preferable than plain variables from parent scope because there's no risk to cross-contaminate tests this way. describe
function doesn't have this context. However, this approach isn't compatible with ES6 arrow functions.
There may be Jasmine helpers that are supposed to be work in conjunction with before*
and it
blocks, e.g. in Angular testing:
beforeAll(fakeAsync(() => {
asyncInitializationThatDoesntNeedDoneCallback();
}));
If it is a constant that should be defined for the entire block, then it can surely be defined without beforeAll
:
describe('...', () => {
const foo = 'test'; // not let
...
});
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