Jest: #5585 breaks pre-established "beforeEach" order

Created on 11 Apr 2018  路  14Comments  路  Source: facebook/jest

When introduced #5885, the order of beforeEachs was inverted. Take the following test for instance:

describe('test', () => {
  beforeEach(() => {
    console.log('BeforeEach');
  });

  it('foo', () => {
    console.log('It Foo');

    beforeEach(() => {
      console.log('BeforeEach Inline Foo');
    });
  });

  it('bar', () => {
    console.log('It Bar');

    beforeEach(() => {
      console.log('BeforeEach Inline Bar');
    });
  });
});

The order should be:

It Foo
BeforeEach Inline Foo
BeforeEach
It Bar

But it is instead:

It Foo
BeforeEach
BeforeEach Inline Foo
It Bar

One might argue about beforeEachs inside its, but the reality is that this is supported and the current order is altered. We need to get some sort of fix or rather revert the PR.

Most helpful comment

@mjesun Oh man, I thought testing it internally meant you'll do it ;) So once again we misunderstood each other. Here's a PR: https://github.com/facebook/jest/pull/6006

Looking forward to seeing it that fixes the issue.

All 14 comments

Why should we support beforeEach inside of tests? Seems super wonky. Is this a case of "we're doing it in thousands of tests at FB, so can't change it"? If so, is it possible to set up a codemod fixing it? If anything, I'd expect it to break completely and not be supported. The fact ordering changed for something that's not documented shouldn't be cause for revert, IMO.

Also, where is BeforeEach Inline Bar? the whole thing seems buggy :P

/cc @niieani

Well, we've altered the process of adding on-the-fly beforeEachs into the queue. As explained, it is arguable that this is or isn't a desired thing, but it's currently supported and broken, so we should fix it.

BeforeEach Inline Bar does not appear because it would be a beforeEach executed after the following test case. Since bar is the last one, it will never get called.

On a side note, I published 23.0.0-alpha.5r, which is a clone of alpha.5, but with #5885 reverted, so that I could unblock myself internally.

On another side note: this happens with inlined requires; I just oversimplified the code to give a simple test case; but real-life scenarios are way more complex than this.

See my review that identifies the change here: https://github.com/facebook/jest/pull/5885#pullrequestreview-111380249

Changing the order consistently would require moving line 64 between 59 and 60:

https://github.com/facebook/jest/blob/95f7813a8cc8f9988da2a9727bd7f8845348edfc/packages/jest-jasmine2/src/tree_processor.js#L59-L70

We could also parametrize it for the top-suite case so its evaluated early, but I don't think keeping inconsistent behavior is the way to go.

Please advise on how to continue.

it's currently supported and broken, so we should fix it

@mjesun is it broken in the 22.x range or is it only in the 23 alpha?

@rickhanlonii this is in the latest alpha only.

I'm OK with moving the wrapChildren to the outer scope; I can also perform a quick test with your change internally and see if everything results in the expected behavior. 馃檪

We could also parametrize it for the top-suite case so its evaluated early, but I don't think keeping inconsistent behavior is the way to go.

No, you are right, whatever the behavior is, it should always be the same.

@niieani any updates regarding the change? We're going to release alpha.7 soon and it'd be nice to include that fix 馃檪

Hey @mjesun, I think we misunderstood each other 馃槃

I was waiting for your quick test:

I can also perform a quick test with your change internally and see if everything results in the expected behavior

Did you try and see if that change works as intended?

Oh, I was waiting for the change 馃槄I need to merge and create alpha.7, then I can test it internally. I agree it was confusing 馃檪

So... are you finally doing the fix? I need to revert if not :(

@mjesun Oh man, I thought testing it internally meant you'll do it ;) So once again we misunderstood each other. Here's a PR: https://github.com/facebook/jest/pull/6006

Looking forward to seeing it that fixes the issue.

Thanks for the fix! 馃檪

Was this page helpful?
0 / 5 - 0 ratings