首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >beforeEach和beforeAll的执行顺序是什么?

beforeEach和beforeAll的执行顺序是什么?
EN

Stack Overflow用户
提问于 2019-07-29 16:35:58
回答 1查看 3.5K关注 0票数 6

我正在使用Jest-Puppeteer对Rails应用程序进行end2end测试。在这些测试之前,我想运行一些种子,并在每次测试之前告诉服务器转到特定的URL。

代码语言:javascript
复制
// imports

describe("user can", () => {
  // here are some constants

  let page;

  beforeAll(async () => {
    await executeSeed(const1);
    await executeSeed(const2);
    await executeSeed(const3);

    page = await newPrivatePage();
    await login(page);
  });

  beforeEach(async () => {
    await page.goto(baseUrl("/some-route"));
  });

  describe("view some great files", () => {

});

我希望种子首先被执行,因为这是beforeAll,如果第一个测试完成,beforeEach将再次执行,但我在jest (https://jestjs.io/docs/en/api#beforeallfn-timeout)的文档中找不到它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-29 17:12:56

您可以在Jest文档上阅读这篇文章https://jestjs.io/docs/en/setup-teardown.html

代码语言:javascript
复制
beforeAll(() => console.log('1 - beforeAll'));
afterAll(() => console.log('1 - afterAll'));
beforeEach(() => console.log('1 - beforeEach'));
afterEach(() => console.log('1 - afterEach'));
test('', () => console.log('1 - test'));
describe('Scoped / Nested block', () => {
  beforeAll(() => console.log('2 - beforeAll'));
  afterAll(() => console.log('2 - afterAll'));
  beforeEach(() => console.log('2 - beforeEach'));
  afterEach(() => console.log('2 - afterEach'));
  test('', () => console.log('2 - test'));
});

// 1 - beforeAll
// 1 - beforeEach
// 1 - test
// 1 - afterEach
// 2 - beforeAll
// 1 - beforeEach
// 2 - beforeEach
// 2 - test
// 2 - afterEach
// 1 - afterEach
// 2 - afterAll
// 1 - afterAll

如您所见,beforeAll将在执行所有测试之前运行。beforeEach将在您的每个测试之前运行。因此,beforeAll将在beforeEach之前运行

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57249976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档