Skip to content

Commit

Permalink
cherry-pick(release-1.14): make sure we always teardown all fixtures (#…
Browse files Browse the repository at this point in the history
…8171)

PR #8158 SHA d32d50a

Co-authored-by: Dmitry Gozman <[email protected]>
  • Loading branch information
aslushnikov and dgozman authored Aug 12, 2021
1 parent 4bdc06c commit cba2f05
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,23 @@ export class FixtureRunner {
}

async teardownScope(scope: FixtureScope) {
let error: Error | undefined;
// Teardown fixtures in the reverse order.
const fixtures = Array.from(this.instanceForId.values()).reverse();
for (const fixture of fixtures) {
if (fixture.registration.scope === scope)
await fixture.teardown();
if (fixture.registration.scope === scope) {
try {
await fixture.teardown();
} catch (e) {
if (error === undefined)
error = e;
}
}
}
if (scope === 'test')
this.testScopeClean = true;
if (error !== undefined)
throw error;
}

async resolveParametersAndRunHookOrTest(fn: Function, workerInfo: WorkerInfo, testInfo: TestInfo | undefined) {
Expand Down
52 changes: 52 additions & 0 deletions tests/playwright-test/hooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,55 @@ test('beforeAll failure should prevent the test, but not afterAll', async ({ run
'%%afterAll',
]);
});

test('fixture error should not prevent afterAll', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.test.js': `
const test = pwt.test.extend({
foo: async ({}, use) => {
await use('foo');
throw new Error('bad fixture');
},
});
test('good test', ({ foo }) => {
console.log('\\n%%test');
});
test.afterAll(() => {
console.log('\\n%%afterAll');
});
`,
});
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
expect(result.output).toContain('bad fixture');
expect(result.output.split('\n').filter(line => line.startsWith('%%'))).toEqual([
'%%test',
'%%afterAll',
]);
});

test('afterEach failure should not prevent afterAll', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.test.js': `
const { test } = pwt;
test('good test', ({ }) => {
console.log('\\n%%test');
});
test.afterEach(() => {
console.log('\\n%%afterEach');
throw new Error('bad afterEach');
})
test.afterAll(() => {
console.log('\\n%%afterAll');
});
`,
});
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
expect(result.output).toContain('bad afterEach');
expect(result.output.split('\n').filter(line => line.startsWith('%%'))).toEqual([
'%%test',
'%%afterEach',
'%%afterAll',
]);
});

0 comments on commit cba2f05

Please sign in to comment.