简体   繁体   English

在描述内部的beforeEach的开始之前,描述外部的beforeEach会始终完成吗?

[英]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? 是否beforeEach保证外部的beforeEach在内部的beforeEach开始之前完成?

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: 是的,保证外部的beforeEach在内部的beforeEach开始之前完成:

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 . Jest通过从定义规范的位置开始并在此处向上移动父级,然后在此处返回反向列表, 找到该规范的所有before函数。

The before functions, test, and after functions get put in an array here . before函数,test和after函数放在此处的数组中。

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 . 每个函数都由mapper包装,该mapper返回一个Promise, 直到该函数完成,取消测试,发生错误或达到超时 ,并且在此处reduce链接在一起的Promise 才解决

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. 因此,除非出现错误,超时或测试被取消, beforeEach函数将按顺序运行至完成,并且上述测试将通过。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 beforeAll/beforeEach afterAll/afterEach 应该在`describe`里面? - beforeAll/beforeEach afterAll/afterEach should be inside `describe`? 使用嵌套的 describe 块调用 beforeEach 和 afterEach - Calling beforeEach and afterEach with nested describe blocks 当你可以在#describe范围内运行代码时,Mocha的#beforeEach的目的是什么? - What is the purpose of Mocha's #beforeEach when you can just run code inside the #describe scope? Jest - 在 describe 块中导入多个测试,重用 beforeEach() 中定义的变量 - Jest - import multiple tests in a describe block, reusing variables defined in beforeEach() 自动向茉莉花描述函数中的beforeEach和afterEach添加代码 - Automatically adding code to beforeEach and afterEach in jasmine describe function 摩卡的全局“before”和“beforeEach”? - Global `before` and `beforeEach` for mocha? Mocha'this'在之前和之前都是钩子 - Mocha 'this' in before and beforeEach hooks 运行jasmine测试时,我怎么知道我是否在一个描述块中,在每个块之前还是阻塞? - When running jasmine tests, how can I know if I am in a describe block, beforeEach block or it block? `before()` 和 `beforeEach()` 有什么区别? - What is the difference between `before()` and `beforeEach()`? 如何在`before`钩子之前运行beforeEach - how to run beforeEach before `before` hook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM