question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider running `beforeEach` before nested `beforeAll`.

See original GitHub issue

Feature 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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
lackoviccommented, Oct 10, 2019

Should also make beforeEach execute only once before each block in its root scope - not before each test function contained in every sub-block. e.g.:

describe('root', () => {
  beforeAll(() => console.log('beforeAll root'));
  beforeEach(() => console.log('beforeEach root'));

  test('1', () => console.log('1'));

  describe('nested', () => {
    beforeAll(() => console.log('beforeAll nested'));
    beforeEach(() => console.log('beforeEach nested'));

    test('2', () => console.log('2'));
    test('3', () => console.log('3'));
  });
});

will print:

beforeAll root
beforeEach root
1
beforeEach root
beforeAll nested
beforeEach nested
2
beforeEach nested
3

which makes perfect sense to me.

2reactions
sgronblocommented, Jun 4, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found