Skip to content

Commit

Permalink
cherry-pick(release-1.14): group fixture initialization under before …
Browse files Browse the repository at this point in the history
…hooks PR #8072
  • Loading branch information
pavelfeldman committed Aug 20, 2021
1 parent a83dad5 commit 8d77dcb
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/src/test-advanced-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ const config = {
reuseExistingServer: !process.env.CI,
},
};
mode.exports = config;
module.exports = config;
```

Now you can use a relative path when navigating the page, or use `baseURL` fixture:
Expand Down
7 changes: 5 additions & 2 deletions src/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { formatLocation, wrapInPromise } from './util';
import * as crypto from 'crypto';
import { FixturesWithLocation, Location, WorkerInfo, TestInfo } from './types';
import { FixturesWithLocation, Location, WorkerInfo, TestInfo, CompleteStepCallback } from './types';

type FixtureScope = 'test' | 'worker';
type FixtureRegistration = {
Expand Down Expand Up @@ -242,7 +242,7 @@ export class FixtureRunner {
throw error;
}

async resolveParametersAndRunHookOrTest(fn: Function, workerInfo: WorkerInfo, testInfo: TestInfo | undefined) {
async resolveParametersAndRunHookOrTest(fn: Function, workerInfo: WorkerInfo, testInfo: TestInfo | undefined, paramsStepCallback?: CompleteStepCallback) {
// Install all automatic fixtures.
for (const registration of this.pool!.registrations.values()) {
const shouldSkip = !testInfo && registration.scope === 'test';
Expand All @@ -259,6 +259,9 @@ export class FixtureRunner {
params[name] = fixture.value;
}

// Report fixture hooks step as completed.
paramsStepCallback?.();

return fn(params, testInfo || workerInfo);
}

Expand Down
16 changes: 11 additions & 5 deletions src/test/workerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,11 @@ export class WorkerRunner extends EventEmitter {
};
if (reportEvents)
this.emit('stepBegin', payload);
let callbackHandled = false;
return (error?: Error | TestError) => {
if (callbackHandled)
return;
callbackHandled = true;
if (error instanceof Error)
error = serializeError(error);
const payload: StepEndPayload = {
Expand Down Expand Up @@ -378,7 +382,6 @@ export class WorkerRunner extends EventEmitter {
}

private async _runBeforeHooks(test: TestCase, testInfo: TestInfoImpl) {
let completeStep: CompleteStepCallback | undefined;
try {
const beforeEachModifiers: Modifier[] = [];
for (let s = test.parent; s; s = s.parent) {
Expand All @@ -390,7 +393,6 @@ export class WorkerRunner extends EventEmitter {
const result = await this._fixtureRunner.resolveParametersAndRunHookOrTest(modifier.fn, this._workerInfo, testInfo);
testInfo[modifier.type](!!result, modifier.description!);
}
completeStep = testInfo._addStep('hook', 'Before Hooks');
await this._runHooks(test.parent!, 'beforeEach', testInfo);
} catch (error) {
if (error instanceof SkipError) {
Expand All @@ -402,19 +404,21 @@ export class WorkerRunner extends EventEmitter {
}
// Continue running afterEach hooks even after the failure.
}
completeStep?.(testInfo.error);
}

private async _runTestWithBeforeHooks(test: TestCase, testInfo: TestInfoImpl) {
const completeStep = testInfo._addStep('hook', 'Before Hooks');
if (test._type === 'test')
await this._runBeforeHooks(test, testInfo);

// Do not run the test when beforeEach hook fails.
if (testInfo.status === 'failed' || testInfo.status === 'skipped')
if (testInfo.status === 'failed' || testInfo.status === 'skipped') {
completeStep?.(testInfo.error);
return;
}

try {
await this._fixtureRunner.resolveParametersAndRunHookOrTest(test.fn, this._workerInfo, testInfo);
await this._fixtureRunner.resolveParametersAndRunHookOrTest(test.fn, this._workerInfo, testInfo, completeStep);
} catch (error) {
if (error instanceof SkipError) {
if (testInfo.status === 'passed')
Expand All @@ -428,6 +432,8 @@ export class WorkerRunner extends EventEmitter {
if (!('error' in testInfo))
testInfo.error = serializeError(error);
}
} finally {
completeStep?.(testInfo.error);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/playwright-test/playwright.expect.misc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ test('should support toHaveURL', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(1);
});

test('should support respect actionTimeout', async ({ runInlineTest }) => {
test('should support respect expect.timeout', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.js': `module.exports = { expect: { timeout: 1000 } }`,
'a.test.ts': `
Expand Down
4 changes: 0 additions & 4 deletions tests/playwright-test/reporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ test('should report expect steps', async ({ runInlineTest }) => {
`%% begin {\"title\":\"After Hooks\",\"category\":\"hook\"}`,
`%% end {\"title\":\"After Hooks\",\"category\":\"hook\"}`,
`%% begin {\"title\":\"Before Hooks\",\"category\":\"hook\"}`,
`%% end {\"title\":\"Before Hooks\",\"category\":\"hook\"}`,
`%% begin {\"title\":\"browserContext.newPage\",\"category\":\"pw:api\"}`,
`%% end {\"title\":\"browserContext.newPage\",\"category\":\"pw:api\"}`,
`%% end {\"title\":\"Before Hooks\",\"category\":\"hook\",\"steps\":[{\"title\":\"browserContext.newPage\",\"category\":\"pw:api\"}]}`,
Expand Down Expand Up @@ -278,7 +277,6 @@ test('should report api steps', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(0);
expect(result.output.split('\n').filter(line => line.startsWith('%%')).map(stripEscapedAscii)).toEqual([
`%% begin {\"title\":\"Before Hooks\",\"category\":\"hook\"}`,
`%% end {\"title\":\"Before Hooks\",\"category\":\"hook\"}`,
`%% begin {\"title\":\"browserContext.newPage\",\"category\":\"pw:api\"}`,
`%% end {\"title\":\"browserContext.newPage\",\"category\":\"pw:api\"}`,
`%% end {\"title\":\"Before Hooks\",\"category\":\"hook\",\"steps\":[{\"title\":\"browserContext.newPage\",\"category\":\"pw:api\"}]}`,
Expand Down Expand Up @@ -326,7 +324,6 @@ test('should report api step failure', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(1);
expect(result.output.split('\n').filter(line => line.startsWith('%%')).map(stripEscapedAscii)).toEqual([
`%% begin {\"title\":\"Before Hooks\",\"category\":\"hook\"}`,
`%% end {\"title\":\"Before Hooks\",\"category\":\"hook\"}`,
`%% begin {\"title\":\"browserContext.newPage\",\"category\":\"pw:api\"}`,
`%% end {\"title\":\"browserContext.newPage\",\"category\":\"pw:api\"}`,
`%% end {\"title\":\"Before Hooks\",\"category\":\"hook\",\"steps\":[{\"title\":\"browserContext.newPage\",\"category\":\"pw:api\"}]}`,
Expand Down Expand Up @@ -362,7 +359,6 @@ test('should report test.step', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(1);
expect(result.output.split('\n').filter(line => line.startsWith('%%')).map(stripEscapedAscii)).toEqual([
`%% begin {\"title\":\"Before Hooks\",\"category\":\"hook\"}`,
`%% end {\"title\":\"Before Hooks\",\"category\":\"hook\"}`,
`%% begin {\"title\":\"browserContext.newPage\",\"category\":\"pw:api\"}`,
`%% end {\"title\":\"browserContext.newPage\",\"category\":\"pw:api\"}`,
`%% end {\"title\":\"Before Hooks\",\"category\":\"hook\",\"steps\":[{\"title\":\"browserContext.newPage\",\"category\":\"pw:api\"}]}`,
Expand Down

0 comments on commit 8d77dcb

Please sign in to comment.