Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(test runner): hide beforeAll/afterAll hooks from the reporter api (#11306)

Dmitry Gozman14fd837e948f
diff --git a/docs/src/test-reporter-api/class-suite.md b/docs/src/test-reporter-api/class-suite.md
index 510be9a59f..b0b3d74c9e 100644
--- a/docs/src/test-reporter-api/class-suite.md
+++ b/docs/src/test-reporter-api/class-suite.md
@@ -24,11 +24,6 @@ Reporter is given a root suite in the [`method: Reporter.onBegin`] method.
Returns the list of all test cases in this suite and its descendants, as opposite to [`property: Suite.tests`].
-## property: Suite.hooks
-- type: <[Array]<[TestCase]>>
-
-`beforeAll` and `afterAll` hooks in the suite. Note that other hooks such as `beforeEach` and `afterEach` are reported as the steps within the test.
-
## property: Suite.location
- type: <[void]|[Location]>
diff --git a/packages/playwright-test/src/dispatcher.ts b/packages/playwright-test/src/dispatcher.ts
index 366b38c586..cf6514249e 100644
--- a/packages/playwright-test/src/dispatcher.ts
+++ b/packages/playwright-test/src/dispatcher.ts
@@ -184,7 +184,8 @@ export class Dispatcher {
data.resultByWorkerIndex.set(worker.workerIndex, { result, stepStack: new Set(), steps: new Map() });
result.workerIndex = worker.workerIndex;
result.startTime = new Date(params.startWallTime);
- this._reporter.onTestBegin?.(data.test, result);
+ if (data.test._type === 'test')
+ this._reporter.onTestBegin?.(data.test, result);
};
worker.addListener('testBegin', onTestBegin);
@@ -308,7 +309,8 @@ export class Dispatcher {
result = runData.result;
} else {
result = data.test._appendTestResult();
- this._reporter.onTestBegin?.(test, result);
+ if (test._type === 'test')
+ this._reporter.onTestBegin?.(test, result);
}
result.error = params.fatalError;
result.status = first ? 'failed' : 'skipped';
@@ -354,9 +356,9 @@ export class Dispatcher {
return true;
// Emulate a "skipped" run, and drop this test from remaining.
- const data = this._testById.get(test._id)!;
- const result = data.test._appendTestResult();
- this._reporter.onTestBegin?.(test, result);
+ const result = test._appendTestResult();
+ if (test._type === 'test')
+ this._reporter.onTestBegin?.(test, result);
result.status = 'skipped';
this._reportTestEnd(test, result);
return false;
@@ -440,7 +442,8 @@ export class Dispatcher {
private _reportTestEnd(test: TestCase, result: TestResult) {
if (test._type === 'test' && result.status !== 'skipped' && result.status !== test.expectedStatus)
++this._failureCount;
- this._reporter.onTestEnd?.(test, result);
+ if (test._type === 'test')
+ this._reporter.onTestEnd?.(test, result);
const maxFailures = this._loader.fullConfig().maxFailures;
if (maxFailures && this._failureCount === maxFailures)
this.stop().catch(e => {});
diff --git a/packages/playwright-test/src/reporters/json.ts b/packages/playwright-test/src/reporters/json.ts
index 6c1c41cdec..e0370b529d 100644
--- a/packages/playwright-test/src/reporters/json.ts
+++ b/packages/playwright-test/src/reporters/json.ts
@@ -42,7 +42,6 @@ export interface JSONReportSuite {
column: number;
line: number;
specs: JSONReportSpec[];
- hooks: JSONReportSpec[];
suites?: JSONReportSuite[];
}
export interface JSONReportSpec {
@@ -208,7 +207,6 @@ class JSONReporter implements Reporter {
title: suite.title,
...this._relativeLocation(suite.location),
specs: suite.tests.map(test => this._serializeTestSpec(test)),
- hooks: suite.hooks.map(test => this._serializeTestSpec(test)),
suites: suites.length ? suites : undefined,
};
}
diff --git a/packages/playwright-test/src/reporters/raw.ts b/packages/playwright-test/src/reporters/raw.ts
index cd06bb6b48..ab8ca6a633 100644
--- a/packages/playwright-test/src/reporters/raw.ts
+++ b/packages/playwright-test/src/reporters/raw.ts
@@ -190,7 +190,7 @@ class RawReporter {
location,
suites: suite.suites.map(s => this._serializeSuite(s)),
tests: suite.tests.map(t => this._serializeTest(t, fileId)),
- hooks: suite.hooks.map(t => this._serializeTest(t, fileId)),
+ hooks: [],
};
}
diff --git a/packages/playwright-test/types/testReporter.d.ts b/packages/playwright-test/types/testReporter.d.ts
index a6d328e08e..18f92b26fc 100644
--- a/packages/playwright-test/types/testReporter.d.ts
+++ b/packages/playwright-test/types/testReporter.d.ts
@@ -84,11 +84,6 @@ export interface Suite {
* listed in the child [suite.suites](https://playwright.dev/docs/api/class-suite#suite-suites).
*/
tests: TestCase[];
- /**
- * `beforeAll` and `afterAll` hooks in the suite. Note that other hooks such as `beforeEach` and `afterEach` are reported
- * as the steps within the test.
- */
- hooks: TestCase[];
/**
* Returns a list of titles from the root down to this suite.
*/
diff --git a/tests/playwright-test/hooks.spec.ts b/tests/playwright-test/hooks.spec.ts
index c8b70d7c3d..47593c7a17 100644
--- a/tests/playwright-test/hooks.spec.ts
+++ b/tests/playwright-test/hooks.spec.ts
@@ -587,28 +587,3 @@ test('should not hang and report results when worker process suddenly exits duri
expect(stripAscii(result.output)).toContain('[1/1] a.spec.js:6:7 › passed');
expect(stripAscii(result.output)).toContain('[1/1] a.spec.js:7:12 › afterAll');
});
-
-test('same beforeAll running in parallel should be reported correctly', async ({ runInlineTest }) => {
- const result = await runInlineTest({
- 'a.spec.js': `
- const { test } = pwt;
- test.describe.parallel('', () => {
- test.beforeAll(async () => {
- await new Promise(f => setTimeout(f, 3000));
- });
- test('test1', () => {});
- test('test2', () => {});
- });
- `
- });
- expect(result.exitCode).toBe(0);
- expect(result.passed).toBe(2);
- const hook = result.report.suites[0].suites[0].hooks[0];
- expect(hook.title).toBe('beforeAll');
- expect(hook.tests.length).toBe(1);
- expect(hook.tests[0].results.length).toBe(2);
- expect(hook.tests[0].results[0].status).toBe('passed');
- expect(hook.tests[0].results[1].status).toBe('passed');
- const workers = [hook.tests[0].results[0].workerIndex, hook.tests[0].results[1].workerIndex];
- expect(workers.sort()).toEqual([0, 1]);
-});
diff --git a/tests/playwright-test/reporter-html.spec.ts b/tests/playwright-test/reporter-html.spec.ts
index a6a36c014b..146c7f7fa8 100644
--- a/tests/playwright-test/reporter-html.spec.ts
+++ b/tests/playwright-test/reporter-html.spec.ts
@@ -325,45 +325,6 @@ test('should render annotations', async ({ runInlineTest, page, showReport }) =>
await expect(page.locator('.test-case-annotation')).toHaveText('skip: I am not interested in this test');
});
-test('should render beforeAll/afterAll hooks', async ({ runInlineTest, page, showReport }) => {
- const result = await runInlineTest({
- 'a.test.js': `
- const { test } = pwt;
- test.use({ trace: 'on' });
- test.beforeAll(async () => {
- });
- test.afterAll(async ({ browser }) => {
- const page = await browser.newPage();
- await page.close();
- await test.step('after step', () => {
- throw new Error('oh!');
- });
- });
- test('test', async ({}) => {
- });
- `,
- }, { reporter: 'dot,html' });
- expect(result.exitCode).toBe(1);
- expect(result.passed).toBe(1);
-
- await showReport();
- await expect(page.locator('.subnav-item:has-text("All") .counter')).toHaveText('1');
- await expect(page.locator('.subnav-item:has-text("Passed") .counter')).toHaveText('1');
- await expect(page.locator('.subnav-item:has-text("Failed") .counter')).toHaveText('0');
- await expect(page.locator('.subnav-item:has-text("Flaky") .counter')).toHaveText('0');
- await expect(page.locator('.subnav-item:has-text("Skipped") .counter')).toHaveText('0');
-
- await expect(page.locator('text=beforeAll')).toBeVisible();
- await expect(page.locator('.test-file-test:has-text("beforeAll") svg.color-icon-success')).toHaveCount(1);
-
- await expect(page.locator('text=afterAll')).toBeVisible();
- await expect(page.locator('.test-file-test:has-text("afterAll") svg.color-text-danger')).toHaveCount(1);
-
- await page.click('text=afterAll');
- await expect(page.locator('.tree-item:has-text("after step") svg.color-text-danger')).toHaveCount(1);
- await expect(page.locator('img')).toBeVisible();
-});
-
test('should render text attachments as text', async ({ runInlineTest, page, showReport }) => {
const result = await runInlineTest({
'a.test.js': `
diff --git a/tests/playwright-test/retry.spec.ts b/tests/playwright-test/retry.spec.ts
index 5e1dcf0c4b..af989a1f0e 100644
--- a/tests/playwright-test/retry.spec.ts
+++ b/tests/playwright-test/retry.spec.ts
@@ -162,7 +162,7 @@ test('should retry beforeAll failure', async ({ runInlineTest }) => {
expect(result.passed).toBe(0);
expect(result.failed).toBe(1);
expect(result.skipped).toBe(1);
- expect(stripAscii(result.output).split('\n')[2]).toBe('××°F×°FF°');
+ expect(stripAscii(result.output).split('\n')[2]).toBe('×°×°F°');
expect(result.output).toContain('BeforeAll is bugged!');
});
diff --git a/tests/playwright-test/stdio.spec.ts b/tests/playwright-test/stdio.spec.ts
index 01e931ba69..434e1988f8 100644
--- a/tests/playwright-test/stdio.spec.ts
+++ b/tests/playwright-test/stdio.spec.ts
@@ -61,19 +61,6 @@ test('should get stdio from worker fixture teardown', async ({ runInlineTest })
]);
});
-test('should get stdio from beforeAll and afterAll', async ({ runInlineTest }) => {
- const result = await runInlineTest({
- 'a.spec.js': `
- const { test } = pwt;
- test.beforeAll(() => console.log('before'));
- test('is a test', () => {});
- test.afterAll(() => console.error('after'));
- `
- });
- expect(result.report.suites[0].hooks[0].tests[0].results[0].stdout).toEqual([{ text: 'before\n' }]);
- expect(result.report.suites[0].hooks[1].tests[0].results[0].stderr).toEqual([{ text: 'after\n' }]);
-});
-
test('should ignore stdio when quiet', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `
diff --git a/utils/generate_types/overrides-testReporter.d.ts b/utils/generate_types/overrides-testReporter.d.ts
index 4f9937a130..af511e8ab1 100644
--- a/utils/generate_types/overrides-testReporter.d.ts
+++ b/utils/generate_types/overrides-testReporter.d.ts
@@ -29,7 +29,6 @@ export interface Suite {
location?: Location;
suites: Suite[];
tests: TestCase[];
- hooks: TestCase[];
titlePath(): string[];
allTests(): TestCase[];
project(): FullProject | undefined;