我正在使用 Jest-Puppeteer 端到端测试 Rails 应用程序。在这些测试之前,我想运行一些种子并进行 DRY 工作,我告诉服务器在每次测试之前转到某个 URL。
// 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)
您可以在 Jest 文档上阅读这篇文章 https://jestjs.io/docs/en/setup-teardown.html。
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'));
});
// Execution order
// 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
之前运行