简体   繁体   English

BeforeAll 与 BeforeEach。 什么时候使用它们?

[英]BeforeAll vs. BeforeEach. When to use them?

I was recently looking over a co-workers code and I realized that he implements a jest function in a BeforeAll function at the top of the describe call, and then creates a data object in a beforeEach function.我最近在查看一个同事的代码,我意识到他在 describe 调用顶部的 BeforeAll 函数中实现了一个 jest 函数,然后在 beforeEach 函数中创建了一个数据对象。 This made me wonder, what exactly are the differences between BeforeAll and BeforeEach.这让我想知道,BeforeAll 和 BeforeEach 之间到底有什么区别。

It was time... I went to Google!!是时候...我去了谷歌! I did find some articles that helped shed some light on some of the functionality differences between the two.我确实找到了一些文章,有助于阐明两者之间的一些功能差异。

Findings 1 : http://breazeal.com/blog/jasmineBefore.html调查结果 1http ://breazeal.com/blog/jasmineBefore.html

Findings 2 : Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll发现 2@Before、@BeforeClass、@BeforeEach 和 @BeforeAll 之间的区别

Given the articles I found that BeforeAll is called once and only once.鉴于我发现 BeforeAll 被调用一次且仅一次的文章。 While the BeforeEach is called before each individual test.而在每个单独的测试之前调用 BeforeEach。 Which was great!太棒了! I now had a better idea of when it was being called!我现在对何时调用它有了更好的了解!

I also found out that the BeforeAll is best used for initializing code.我还发现 BeforeAll 最适合用于初始化代码。 Which makes perfect sense!这很有意义! Initialize it once.初始化一次。 Boom, you're done.砰,你已经完成了。

My confusion I am having is when is something initialized and when is it not?我的困惑是什么时候初始化什么时候不初始化? I have found that BeforeEach in our code is used more often than not.我发现我们的代码中经常使用 BeforeEach。 What I am curious about is what kind of code is considered to be "initializing" code, vs whatever code should be in the BeforeEach.我很好奇的是,什么样的代码被认为是“初始化”代码,而不是 BeforeEach 中应该包含的任何代码。

An example from our code below:下面是我们代码中的一个示例:

    beforeAll((done) => {
      // Mocking method from within Box file
      transferBoxPlanSpy = jest.spyOn(Box, 'transferPlanFromBox').mockImplementation(() => Promise.resolve());

      // Pulling data from MongoDB
      User.findOne({ user_name: 'testsurgeon1' }, (err, user) => {
        user.addMGSPermission();
        user.save(done);
      });
    });

    beforeEach(() => {
      planData2 = {
        user_name: 'hello1',
        laterality: 'right',
        plan_id: 'testplan42',
        order_number: '856-hd-02-l',
        file_id: '123456sbyuidbefui',
      };
    });

I hope my question isn't too vague.我希望我的问题不会太模糊。 Thank you for your time!感谢您的时间!

Edit 1 I would like to point out that this code was not made by myself, but from one of our members on the software team.编辑 1我想指出,这段代码不是我自己编写的,而是我们软件团队的一位成员编写的。 He puts the object inside of the BeforeEach, and the mocks inside of the BeforeAll.他将对象放在 BeforeEach 中,将 mock 放在 BeforeAll 中。

My confusion is that it seems like all code can be put just into BeforeAll, with a few exceptions.我的困惑是,似乎所有代码都可以放入 BeforeAll 中,但有一些例外。

Both are used to set up whatever conditions are needed for one or more tests. 两者都用于设置一个或多个测试所需的条件。

If you're certain that the tests don't make any changes to those conditions, you can use beforeAll (which will run once). 如果确定测试不会对这些条件进行任何更改,则可以使用beforeAll (它将运行一次)。

If the tests do make changes to those conditions, then you would need to use beforeEach , which will run before every test, so it can reset the conditions for the next one. 如果测试确实改变了这些条件,那么您将需要使用beforeEach ,它将在每次测试之前运行,因此它可以为下一个条件重置条件。

Unless the initialization is slow or computationally expensive, it may be safest to default to using beforeEach as it reduces the opportunity for human error, ie not realizing that one test is changing the setup for the next one. 除非初始化很慢或计算量beforeEach ,否则默认使用beforeEach可能是最安全的方法,因为它减少了人为错误的机会,即没有意识到一个测试正在更改下一个测试的设置。

The sample you showed is a good example of using both in combination -- the slow network call is put in beforeAll , so it only has to happen once; 您显示的示例是将两者结合使用的一个很好的例子-缓慢的网络调用被放入beforeAll ,因此它只需发生一次; and the data object (which is presumably modified by the tests) is reset each time in beforeEach . 并每次在beforeEach重置数据对象(可能由测试修改)。

I know this is an old post but in the event that others come looking I would like to add some important information that I find surprised not to be mentioned here:我知道这是一篇旧帖子,但如果其他人来找我,我想添加一些重要信息,我很惊讶这里没有提到:

That beforeAll is specifically for ASYNCHRONOUS calls that need to complete before the tests are run.beforeAll专门用于需要在运行测试之前完成的 ASYNCHRONOUS 调用。

In the original post the beforeAll function is redundant as it doesn't return a promise - you could simply place the function body immediately before your first describe or test在原始帖子中, beforeAll函数是多余的,因为它不返回承诺 - 您可以简单地将函数体放在第一次describetest之前

See the jest docs: https://jestjs.io/docs/setup-teardown查看笑话文档: https ://jestjs.io/docs/setup-teardown

In some cases, you only need to do setup once, at the beginning of a file.在某些情况下,您只需在文件开头进行一次设置。 This can be especially bothersome when the setup is asynchronous, so you can't do it inline.当设置是异步的时,这可能特别麻烦,因此您不能内联。 Jest provides beforeAll and afterAll to handle this situation. Jest 提供了 beforeAll 和 afterAll 来处理这种情况。

Eg the following returns a promise which will resolve before the tests proceed to run.例如,以下返回一个承诺,它将在测试继续运行之前解决。

beforeAll(() => {
  return initializeCityDatabase();
});

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

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