简体   繁体   中英

Will a beforeEach that is outside a describe always complete before the start of a beforeEach that is inside a describe?

Is the outer beforeEach guaranteed to complete before the inner beforeEach starts?

let qux;

beforeEach(() => {
  //
  // other synchronous code here
  //
  qux = null;
});

describe('Description', () => {

  beforeEach(() => {
    qux = 0;
    //
    // other synchronous code here
    //
  });

  it('Predicate', () => {
    expect(qux).toEqual(0);
  });
});

In other words, is the above test guaranteed to pass?

Yes, the outer beforeEach is guaranteed to complete before the inner beforeEach starts:

Jest finds all the before functions for a spec by starting where the spec is defined and walking up the parents here and returning the reversed list here .

The before functions, test, and after functions get put in an array here .

Each function is wrapped by mapper which returns a Promise that does not resolve until the function completes, the test is cancelled, an error occurs, or the timeout is reached , and the resulting Promises are chained together by the reduce here .

So unless there is an error, a timeout, or the test is cancelled the beforeEach functions will run to completion in order and the test above will pass.

PR中有一些信息,它增加了对多个beforeEach / afterEach调用的支持( https://github.com/qunitjs/qunit/pull/1188/files)-只要您的操作是同步的,就可以了-您也可以在上述PR的测试案例中进行验证。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM