Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(testing): allow to re-use pages across it blocks #5830

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 '@stencil/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();
});
});