mirror of
https://github.com/microsoft/playwright.git
synced 2024-12-15 06:02:57 +03:00
87548f94c1
Each hook gets its own test scope. This is not too useful for object fixtures like `page` (although one can use a page in `beforeAll` to save storage state), but much more useful for option fixtures like `viewport`.
245 lines
7.1 KiB
TypeScript
245 lines
7.1 KiB
TypeScript
/**
|
|
* Copyright Microsoft Corporation. All rights reserved.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import { test, expect } from './playwright-test-fixtures';
|
|
|
|
test('hooks should work with fixtures', async ({ runInlineTest }) => {
|
|
const { results } = await runInlineTest({
|
|
'helper.ts': `
|
|
global.logs = [];
|
|
let counter = 0;
|
|
export const test = pwt.test.extend({
|
|
w: [ async ({}, run) => {
|
|
global.logs.push('+w');
|
|
await run(17);
|
|
global.logs.push('-w');
|
|
}, { scope: 'worker' }],
|
|
|
|
t: async ({}, run) => {
|
|
global.logs.push('+t');
|
|
await run(42 + counter);
|
|
++counter;
|
|
global.logs.push('-t');
|
|
},
|
|
});
|
|
`,
|
|
'a.test.js': `
|
|
const { test } = require('./helper');
|
|
test.describe('suite', () => {
|
|
test.beforeAll(async ({ w, t }) => {
|
|
global.logs.push('beforeAll-' + w + '-' + t);
|
|
});
|
|
test.afterAll(async ({ w, t }) => {
|
|
global.logs.push('afterAll-' + w + '-' + t);
|
|
});
|
|
|
|
test.beforeEach(async ({ w, t }) => {
|
|
global.logs.push('beforeEach-' + w + '-' + t);
|
|
});
|
|
test.afterEach(async ({ w, t }) => {
|
|
global.logs.push('afterEach-' + w + '-' + t);
|
|
});
|
|
|
|
test('one', async ({ w, t }) => {
|
|
global.logs.push('test-' + w + '-' + t);
|
|
});
|
|
});
|
|
|
|
test('two', async ({ t }) => {
|
|
expect(global.logs).toEqual([
|
|
'+w',
|
|
'+t',
|
|
'beforeAll-17-42',
|
|
'-t',
|
|
'+t',
|
|
'beforeEach-17-43',
|
|
'test-17-43',
|
|
'afterEach-17-43',
|
|
'-t',
|
|
'+t',
|
|
'afterAll-17-44',
|
|
'-t',
|
|
'+t',
|
|
]);
|
|
});
|
|
`,
|
|
});
|
|
expect(results[0].status).toBe('passed');
|
|
});
|
|
|
|
test('afterEach failure should not prevent other hooks and fixtures teardown', async ({ runInlineTest }) => {
|
|
const report = await runInlineTest({
|
|
'helper.ts': `
|
|
global.logs = [];
|
|
export const test = pwt.test.extend({
|
|
foo: async ({}, run) => {
|
|
console.log('+t');
|
|
await run();
|
|
console.log('-t');
|
|
}
|
|
});
|
|
`,
|
|
'a.test.js': `
|
|
const { test } = require('./helper');
|
|
test.describe('suite', () => {
|
|
test.afterEach(async () => {
|
|
console.log('afterEach1');
|
|
});
|
|
test.afterEach(async () => {
|
|
console.log('afterEach2');
|
|
throw new Error('afterEach2');
|
|
});
|
|
test('one', async ({foo}) => {
|
|
console.log('test');
|
|
expect(true).toBe(true);
|
|
});
|
|
});
|
|
`,
|
|
});
|
|
expect(report.output).toContain('+t\ntest\nafterEach2\nafterEach1\n-t');
|
|
expect(report.results[0].error.message).toContain('afterEach2');
|
|
});
|
|
|
|
test('beforeEach failure should prevent the test, but not other hooks', async ({ runInlineTest }) => {
|
|
const report = await runInlineTest({
|
|
'a.test.js': `
|
|
const { test } = pwt;
|
|
test.describe('suite', () => {
|
|
test.beforeEach(async ({}) => {
|
|
console.log('beforeEach1');
|
|
});
|
|
test.beforeEach(async ({}) => {
|
|
console.log('beforeEach2');
|
|
throw new Error('beforeEach2');
|
|
});
|
|
test.afterEach(async ({}) => {
|
|
console.log('afterEach');
|
|
});
|
|
test('one', async ({}) => {
|
|
console.log('test');
|
|
});
|
|
});
|
|
`,
|
|
});
|
|
expect(report.output).toContain('beforeEach1\nbeforeEach2\nafterEach');
|
|
expect(report.results[0].error.message).toContain('beforeEach2');
|
|
});
|
|
|
|
test('beforeAll should be run once', async ({ runInlineTest }) => {
|
|
const report = await runInlineTest({
|
|
'a.test.js': `
|
|
const { test } = pwt;
|
|
test.describe('suite1', () => {
|
|
let counter = 0;
|
|
test.beforeAll(async () => {
|
|
console.log('beforeAll1-' + (++counter));
|
|
});
|
|
test.describe('suite2', () => {
|
|
test.beforeAll(async () => {
|
|
console.log('beforeAll2');
|
|
});
|
|
test('one', async ({}) => {
|
|
console.log('test');
|
|
});
|
|
});
|
|
});
|
|
`,
|
|
});
|
|
expect(report.output).toContain('beforeAll1-1\nbeforeAll2\ntest');
|
|
});
|
|
|
|
test('beforeEach should be able to skip a test', async ({ runInlineTest }) => {
|
|
const { passed, skipped, exitCode } = await runInlineTest({
|
|
'a.test.js': `
|
|
const { test } = pwt;
|
|
test.beforeEach(async ({}, testInfo) => {
|
|
testInfo.skip(testInfo.title === 'test2');
|
|
});
|
|
test('test1', async () => {});
|
|
test('test2', async () => {});
|
|
`,
|
|
});
|
|
expect(exitCode).toBe(0);
|
|
expect(passed).toBe(1);
|
|
expect(skipped).toBe(1);
|
|
});
|
|
|
|
test('beforeAll from a helper file should throw', async ({ runInlineTest }) => {
|
|
const result = await runInlineTest({
|
|
'my-test.ts': `
|
|
export const test = pwt.test;
|
|
test.beforeAll(() => {});
|
|
`,
|
|
'playwright.config.ts': `
|
|
import { test } from './my-test';
|
|
`,
|
|
'a.test.ts': `
|
|
import { test } from './my-test';
|
|
test('should work', async () => {
|
|
});
|
|
`,
|
|
});
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.output).toContain('beforeAll hook can only be called in a test file');
|
|
});
|
|
|
|
test('beforeAll hooks are skipped when no tests in the suite are run', async ({ runInlineTest }) => {
|
|
const result = await runInlineTest({
|
|
'a.test.js': `
|
|
const { test } = pwt;
|
|
test.describe('suite1', () => {
|
|
test.beforeAll(() => {
|
|
console.log('\\n%%beforeAll1');
|
|
});
|
|
test('skipped', () => {});
|
|
});
|
|
test.describe('suite2', () => {
|
|
test.beforeAll(() => {
|
|
console.log('\\n%%beforeAll2');
|
|
});
|
|
test.only('passed', () => {});
|
|
});
|
|
`,
|
|
});
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.passed).toBe(1);
|
|
expect(result.output).toContain('%%beforeAll2');
|
|
expect(result.output).not.toContain('%%beforeAll1');
|
|
});
|
|
|
|
test('beforeAll hook should get retry index of the first test', async ({ runInlineTest }) => {
|
|
const result = await runInlineTest({
|
|
'a.test.js': `
|
|
const { test } = pwt;
|
|
test.beforeAll(({}, testInfo) => {
|
|
console.log('\\n%%beforeall-retry-' + testInfo.retry);
|
|
});
|
|
test('passed', ({}, testInfo) => {
|
|
console.log('\\n%%test-retry-' + testInfo.retry);
|
|
expect(testInfo.retry).toBe(1);
|
|
});
|
|
`,
|
|
}, { retries: 1 });
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.flaky).toBe(1);
|
|
expect(result.output.split('\n').filter(line => line.startsWith('%%'))).toEqual([
|
|
'%%beforeall-retry-0',
|
|
'%%test-retry-0',
|
|
'%%beforeall-retry-1',
|
|
'%%test-retry-1',
|
|
]);
|
|
});
|