Consider running `beforeEach` before nested `beforeAll`.
See original GitHub issueFeature Proposal
Change how beforeEach
/beforeAll
are ordered to respect nesting.
Motivation
Let’s say I have a test file like this
describe('example', () => {
let someObject;
beforeEach(() => {
someObject = {someProperty: 'This is needed for a test or something.'}
});
it('test one', () => {})
describe('nested', () => {
let someOtherObject;
beforeAll(() => {
someOtherObject = doSomethingWith(someObject.someProperty);
});
it('test two', () => {
expect(someOtherObject).toBeDefined();
});
});
});
I run it and everything passes. But, then I move/xit/comment out “test one”, and “test two” breaks, and it’s not immediately apparent why.
Example
// This should print "example.beforeEach" before "nested.beforeAll" instead of the other way around.
describe('example', () => {
beforeEach(() => console.log('example.beforeEach'));
describe('nested', () => {
beforeAll(() => console.log('nested.beforeAll'));
it('does something.', () => {});
});
});
Pitch
It makes editing test suites more predictable.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Jest's 'beforeEach' may not be running the way you think it ...
The top-beforeAll running first, the top-beforeEach running before each test and then once before the describe block. Then the nested-beforeAll ...
Read more >In what order does beforeEach and beforeAll execute?
As you can see, beforeAll will be run before all of your test be executed. beforeEach will be run before each of you...
Read more >Setup and Teardown - Jest
Note that the top-level beforeEach is executed before the beforeEach inside the describe block. It may help to illustrate the order of execution ......
Read more >Avoid Nesting when you're Testing - Kent C. Dodds
Note: my point isn't that nesting is bad by itself, but rather that it naturally encourages using test hooks (such as beforeEach )...
Read more >Cypress basics: before(), beforeEach(), after() and afterEach()
js. Sometimes I like to use these blocks to run a "global" beforeEach() block that I want to run before all of my...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Should also make
beforeEach
execute only once before each block in its root scope - not before eachtest
function contained in every sub-block. e.g.:will print:
which makes perfect sense to me.
Just wanted to say that I agree with most other commenters that the ordering should be based on the nesting. Inner before outer. Not that beforeAll’s must come before beforeEach. At least it’s shown in the documentation: https://jestjs.io/docs/en/setup-teardown, although it could be made more explicit.