Skip to content

Commit

Permalink
fix(testing): allow to re-use pages across it blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhduy1407 committed Dec 14, 2024
1 parent 297a163 commit ca7046f
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/testing/jest/jest-27-and-under/jest-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export function createJestPuppeteerEnvironment(): JestPuppeteerEnvironmentConstr
this.browser = await connectBrowser();
}

/**
* if the user had open pages before, close them all when creating a new one
*/
await this.closeOpenPages();

const page = await newBrowserPage(this.browser);
this.pages.push(page);
// during E2E tests, we can safely assume that the current environment is a `E2EProcessEnv`
Expand All @@ -40,7 +45,7 @@ export function createJestPuppeteerEnvironment(): JestPuppeteerEnvironmentConstr
}

async closeOpenPages() {
await Promise.all(this.pages.map((page) => page.close()));
await Promise.all(this.pages.filter((page) => !page.isClosed()).map((page) => page.close()));
this.pages.length = 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ export function jestSetupTestFramework() {
});

afterEach(async () => {
if (global.__CLOSE_OPEN_PAGES__) {
await global.__CLOSE_OPEN_PAGES__();
}
stopAutoApplyChanges();

// Remove each node from the mocked DOM
Expand All @@ -61,6 +58,12 @@ export function jestSetupTestFramework() {
global.resourcesUrl = '/build';
});

afterAll(async () => {
if (global.__CLOSE_OPEN_PAGES__) {
await global.__CLOSE_OPEN_PAGES__();
}
});

const jasmineEnv = (jasmine as any).getEnv();
if (jasmineEnv != null) {
jasmineEnv.addReporter({
Expand Down
7 changes: 6 additions & 1 deletion src/testing/jest/jest-28/jest-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export function createJestPuppeteerEnvironment(): JestPuppeteerEnvironmentConstr
this.browser = await connectBrowser();
}

/**
* if the user had open pages before, close them all when creating a new one
*/
await this.closeOpenPages();

const page = await newBrowserPage(this.browser);
this.pages.push(page);
// during E2E tests, we can safely assume that the current environment is a `E2EProcessEnv`
Expand All @@ -81,7 +86,7 @@ export function createJestPuppeteerEnvironment(): JestPuppeteerEnvironmentConstr
}

async closeOpenPages() {
await Promise.all(this.pages.map((page) => page.close()));
await Promise.all(this.pages.filter((page) => !page.isClosed()).map((page) => page.close()));
this.pages.length = 0;
}

Expand Down
9 changes: 6 additions & 3 deletions src/testing/jest/jest-28/jest-setup-test-framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ export function jestSetupTestFramework() {
});

afterEach(async () => {
if (global.__CLOSE_OPEN_PAGES__) {
await global.__CLOSE_OPEN_PAGES__();
}
stopAutoApplyChanges();

// Remove each node from the mocked DOM
Expand All @@ -61,6 +58,12 @@ export function jestSetupTestFramework() {
global.resourcesUrl = '/build';
});

afterAll(async () => {
if (global.__CLOSE_OPEN_PAGES__) {
await global.__CLOSE_OPEN_PAGES__();
}
});

global.screenshotDescriptions = new Set();

// during E2E tests, we can safely assume that the current environment is a `E2EProcessEnv`
Expand Down
7 changes: 6 additions & 1 deletion src/testing/jest/jest-29/jest-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export function createJestPuppeteerEnvironment(): JestPuppeteerEnvironmentConstr
this.browser = await connectBrowser();
}

/**
* if the user had open pages before, close them all when creating a new one
*/
await this.closeOpenPages();

const page = await newBrowserPage(this.browser);
this.pages.push(page);
// during E2E tests, we can safely assume that the current environment is a `E2EProcessEnv`
Expand All @@ -81,7 +86,7 @@ export function createJestPuppeteerEnvironment(): JestPuppeteerEnvironmentConstr
}

async closeOpenPages() {
await Promise.all(this.pages.map((page) => page.close()));
await Promise.all(this.pages.filter((page) => !page.isClosed()).map((page) => page.close()));
this.pages.length = 0;
}

Expand Down
9 changes: 6 additions & 3 deletions src/testing/jest/jest-29/jest-setup-test-framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ export function jestSetupTestFramework() {
});

afterEach(async () => {
if (global.__CLOSE_OPEN_PAGES__) {
await global.__CLOSE_OPEN_PAGES__();
}
stopAutoApplyChanges();

// Remove each node from the mocked DOM
Expand All @@ -61,6 +58,12 @@ export function jestSetupTestFramework() {
global.resourcesUrl = '/build';
});

afterAll(async () => {
if (global.__CLOSE_OPEN_PAGES__) {
await global.__CLOSE_OPEN_PAGES__();
}
});

global.screenshotDescriptions = new Set();

// during E2E tests, we can safely assume that the current environment is a `E2EProcessEnv`
Expand Down
8 changes: 8 additions & 0 deletions src/testing/puppeteer/puppeteer-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,14 @@ export class E2EElement extends MockHTMLElement implements pd.E2EElementInternal

const rootElm = frag.firstElementChild;

/**
* in case the user called `newE2EPage` without any content `rootElm` will be undefined
* and further operations will fail. We need to check for this case and return early.
*/
if (!rootElm) {
return;
}

this.nodeName = rootElm.nodeName;
this.attributes = cloneAttributes(rootElm.attributes);

Expand Down
19 changes: 19 additions & 0 deletions test/end-to-end/src/miscellaneous/test.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { type E2EPage, newE2EPage } from '@rindo/core/testing';

describe('do not throw page already closed if page was defined in before(All) hook', () => {
let page: E2EPage;

beforeAll(async () => {
page = await newE2EPage();
});

it('first test', async () => {
const p = await page.find('html');
expect(p).not.toBeNull();
});

it('second test', async () => {
const p = await page.find('html');
expect(p).not.toBeNull();
});
});

0 comments on commit ca7046f

Please sign in to comment.