2021-06-07 03:09:53 +03:00
|
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-08 02:11:44 +03:00
|
|
|
|
import { test, expect } from './playwright-test-fixtures';
|
2021-06-15 08:45:58 +03:00
|
|
|
|
import * as path from 'path';
|
2021-06-07 03:09:53 +03:00
|
|
|
|
|
|
|
|
|
test('handle long test names', async ({ runInlineTest }) => {
|
|
|
|
|
const title = 'title'.repeat(30);
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'a.test.js': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-06-07 03:09:53 +03:00
|
|
|
|
test('${title}', async ({}) => {
|
|
|
|
|
expect(1).toBe(0);
|
|
|
|
|
});
|
|
|
|
|
`,
|
|
|
|
|
});
|
2023-02-08 02:11:44 +03:00
|
|
|
|
expect(result.output).toContain('expect(1).toBe');
|
2021-06-07 03:09:53 +03:00
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('print the error name', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'a.spec.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-06-07 03:09:53 +03:00
|
|
|
|
test('foobar', async ({}) => {
|
|
|
|
|
const error = new Error('my-message');
|
|
|
|
|
error.name = 'FooBarError';
|
|
|
|
|
throw error;
|
|
|
|
|
});
|
|
|
|
|
`
|
|
|
|
|
});
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
|
expect(result.failed).toBe(1);
|
|
|
|
|
expect(result.output).toContain('FooBarError: my-message');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('print should print the error name without a message', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'a.spec.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-06-07 03:09:53 +03:00
|
|
|
|
test('foobar', async ({}) => {
|
|
|
|
|
const error = new Error();
|
|
|
|
|
error.name = 'FooBarError';
|
|
|
|
|
throw error;
|
|
|
|
|
});
|
|
|
|
|
`
|
|
|
|
|
});
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
|
expect(result.failed).toBe(1);
|
|
|
|
|
expect(result.output).toContain('FooBarError');
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-05 03:00:55 +03:00
|
|
|
|
test('should print an error in a codeframe', async ({ runInlineTest }) => {
|
2021-06-07 03:09:53 +03:00
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'a.spec.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2022-01-05 03:00:55 +03:00
|
|
|
|
test('foobar', async ({}) => {
|
|
|
|
|
const error = new Error('my-message');
|
|
|
|
|
error.name = 'FooBarError';
|
|
|
|
|
throw error;
|
|
|
|
|
});
|
2021-06-07 03:09:53 +03:00
|
|
|
|
`
|
|
|
|
|
}, {}, {
|
|
|
|
|
FORCE_COLOR: '0',
|
|
|
|
|
});
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
|
expect(result.failed).toBe(1);
|
|
|
|
|
expect(result.output).toContain('FooBarError: my-message');
|
2023-02-15 06:20:56 +03:00
|
|
|
|
expect(result.output).not.toContain('at a.spec.ts:5');
|
|
|
|
|
expect(result.output).toContain(` 2 | import { test, expect } from '@playwright/test';`);
|
|
|
|
|
expect(result.output).toContain(` 3 | test('foobar', async ({}) => {`);
|
|
|
|
|
expect(result.output).toContain(`> 4 | const error = new Error('my-message');`);
|
2022-01-05 03:00:55 +03:00
|
|
|
|
});
|
|
|
|
|
|
2022-10-27 01:18:31 +03:00
|
|
|
|
test('should filter out node_modules error in a codeframe', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'node_modules/utils/utils.js': `
|
|
|
|
|
function assert(value) {
|
|
|
|
|
if (!value)
|
|
|
|
|
throw new Error('Assertion error');
|
|
|
|
|
}
|
|
|
|
|
module.exports = { assert };
|
|
|
|
|
`,
|
|
|
|
|
'a.spec.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2022-10-27 01:18:31 +03:00
|
|
|
|
const { assert } = require('utils/utils.js');
|
|
|
|
|
test('fail', async ({}) => {
|
|
|
|
|
assert(false);
|
|
|
|
|
});
|
|
|
|
|
`
|
|
|
|
|
});
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
|
expect(result.failed).toBe(1);
|
2023-02-08 02:11:44 +03:00
|
|
|
|
const output = result.output;
|
2022-10-27 01:18:31 +03:00
|
|
|
|
expect(output).toContain('Error: Assertion error');
|
2023-02-15 06:20:56 +03:00
|
|
|
|
expect(output).toContain('a.spec.ts:4:11 › fail');
|
|
|
|
|
expect(output).toContain(` 4 | test('fail', async ({}) => {`);
|
|
|
|
|
expect(output).toContain(`> 5 | assert(false);`);
|
|
|
|
|
expect(output).toContain(` | ^`);
|
|
|
|
|
expect(output).toContain(`utils.js:4`);
|
|
|
|
|
expect(output).toContain(`a.spec.ts:5:9`);
|
2022-10-27 01:18:31 +03:00
|
|
|
|
});
|
|
|
|
|
|
2022-08-15 19:28:55 +03:00
|
|
|
|
test('should print codeframe from a helper', async ({ runInlineTest }) => {
|
2022-01-05 03:00:55 +03:00
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'helper.ts': `
|
|
|
|
|
export function ohMy() {
|
|
|
|
|
throw new Error('oh my');
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
'a.spec.ts': `
|
|
|
|
|
import { ohMy } from './helper';
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2022-01-05 03:00:55 +03:00
|
|
|
|
test('foobar', async ({}) => {
|
|
|
|
|
ohMy();
|
|
|
|
|
});
|
|
|
|
|
`
|
|
|
|
|
}, {}, {
|
|
|
|
|
FORCE_COLOR: '0',
|
|
|
|
|
});
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
|
expect(result.failed).toBe(1);
|
|
|
|
|
expect(result.output).toContain('Error: oh my');
|
2023-02-15 06:20:56 +03:00
|
|
|
|
expect(result.output).toContain(` 2 | export function ohMy() {`);
|
|
|
|
|
expect(result.output).toContain(` > 3 | throw new Error('oh my');`);
|
2022-08-15 19:28:55 +03:00
|
|
|
|
expect(result.output).toContain(` | ^`);
|
2021-06-07 03:09:53 +03:00
|
|
|
|
});
|
2021-06-15 08:45:58 +03:00
|
|
|
|
|
|
|
|
|
test('should print slow tests', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'playwright.config.ts': `
|
|
|
|
|
module.exports = {
|
|
|
|
|
projects: [
|
|
|
|
|
{ name: 'foo' },
|
|
|
|
|
{ name: 'bar' },
|
|
|
|
|
{ name: 'baz' },
|
|
|
|
|
{ name: 'qux' },
|
|
|
|
|
],
|
|
|
|
|
reportSlowTests: { max: 0, threshold: 500 },
|
|
|
|
|
};
|
|
|
|
|
`,
|
|
|
|
|
'dir/a.test.js': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-06-15 08:45:58 +03:00
|
|
|
|
test('slow test', async ({}) => {
|
|
|
|
|
await new Promise(f => setTimeout(f, 1000));
|
|
|
|
|
});
|
|
|
|
|
`,
|
|
|
|
|
'dir/b.test.js': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-06-15 08:45:58 +03:00
|
|
|
|
test('fast test', async ({}) => {
|
|
|
|
|
await new Promise(f => setTimeout(f, 100));
|
|
|
|
|
});
|
|
|
|
|
`,
|
|
|
|
|
});
|
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
|
expect(result.passed).toBe(8);
|
2023-02-08 02:11:44 +03:00
|
|
|
|
expect(result.output).toContain(`Slow test file: [foo] › dir${path.sep}a.test.js (`);
|
|
|
|
|
expect(result.output).toContain(`Slow test file: [bar] › dir${path.sep}a.test.js (`);
|
|
|
|
|
expect(result.output).toContain(`Slow test file: [baz] › dir${path.sep}a.test.js (`);
|
|
|
|
|
expect(result.output).toContain(`Slow test file: [qux] › dir${path.sep}a.test.js (`);
|
|
|
|
|
expect(result.output).toContain(`Consider splitting slow test files to speed up parallel execution`);
|
|
|
|
|
expect(result.output).not.toContain(`Slow test file: [foo] › dir${path.sep}b.test.js (`);
|
|
|
|
|
expect(result.output).not.toContain(`Slow test file: [bar] › dir${path.sep}b.test.js (`);
|
|
|
|
|
expect(result.output).not.toContain(`Slow test file: [baz] › dir${path.sep}b.test.js (`);
|
|
|
|
|
expect(result.output).not.toContain(`Slow test file: [qux] › dir${path.sep}b.test.js (`);
|
2021-06-15 08:45:58 +03:00
|
|
|
|
});
|
|
|
|
|
|
2022-02-08 07:10:13 +03:00
|
|
|
|
test('should not print slow parallel tests', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'playwright.config.ts': `
|
|
|
|
|
module.exports = {
|
|
|
|
|
reportSlowTests: { max: 0, threshold: 500 },
|
|
|
|
|
};
|
|
|
|
|
`,
|
|
|
|
|
'dir/a.test.js': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2022-02-08 07:10:13 +03:00
|
|
|
|
test.describe.parallel('suite', () => {
|
|
|
|
|
test('inner slow test', async ({}) => {
|
|
|
|
|
await new Promise(f => setTimeout(f, 1000));
|
|
|
|
|
});
|
|
|
|
|
test('inner fast test', async ({}) => {
|
|
|
|
|
await new Promise(f => setTimeout(f, 100));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
`,
|
|
|
|
|
});
|
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
|
expect(result.passed).toBe(2);
|
2023-02-08 02:11:44 +03:00
|
|
|
|
expect(result.output).not.toContain('Slow test file');
|
2022-02-08 07:10:13 +03:00
|
|
|
|
});
|
|
|
|
|
|
2021-06-15 08:45:58 +03:00
|
|
|
|
test('should not print slow tests', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'playwright.config.ts': `
|
|
|
|
|
module.exports = {
|
|
|
|
|
projects: [
|
|
|
|
|
{ name: 'baz' },
|
|
|
|
|
{ name: 'qux' },
|
|
|
|
|
],
|
|
|
|
|
reportSlowTests: null,
|
|
|
|
|
};
|
|
|
|
|
`,
|
|
|
|
|
'dir/a.test.js': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-06-15 08:45:58 +03:00
|
|
|
|
test('slow test', async ({}) => {
|
|
|
|
|
await new Promise(f => setTimeout(f, 1000));
|
|
|
|
|
});
|
|
|
|
|
test('fast test', async ({}) => {
|
|
|
|
|
await new Promise(f => setTimeout(f, 100));
|
|
|
|
|
});
|
|
|
|
|
`,
|
|
|
|
|
});
|
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
|
expect(result.passed).toBe(4);
|
2023-02-08 02:11:44 +03:00
|
|
|
|
expect(result.output).not.toContain('Slow test');
|
2021-06-15 08:45:58 +03:00
|
|
|
|
});
|
2021-08-12 02:44:19 +03:00
|
|
|
|
|
2021-08-20 04:20:53 +03:00
|
|
|
|
test('should print flaky failures', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'a.spec.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-08-20 04:20:53 +03:00
|
|
|
|
test('foobar', async ({}, testInfo) => {
|
|
|
|
|
expect(testInfo.retry).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
`
|
|
|
|
|
}, { retries: '1', reporter: 'list' });
|
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
|
expect(result.flaky).toBe(1);
|
2023-02-08 02:11:44 +03:00
|
|
|
|
expect(result.output).toContain('expect(testInfo.retry).toBe(1)');
|
2021-08-20 04:20:53 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('should print flaky timeouts', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'a.spec.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-08-20 04:20:53 +03:00
|
|
|
|
test('foobar', async ({}, testInfo) => {
|
|
|
|
|
if (!testInfo.retry)
|
|
|
|
|
await new Promise(f => setTimeout(f, 2000));
|
|
|
|
|
});
|
|
|
|
|
`
|
|
|
|
|
}, { retries: '1', reporter: 'list', timeout: '1000' });
|
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
|
expect(result.flaky).toBe(1);
|
2023-02-08 02:11:44 +03:00
|
|
|
|
expect(result.output).toContain('Test timeout of 1000ms exceeded.');
|
2021-08-20 04:20:53 +03:00
|
|
|
|
});
|
2021-08-28 17:19:45 +03:00
|
|
|
|
|
|
|
|
|
test('should print stack-less errors', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'a.spec.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-08-28 17:19:45 +03:00
|
|
|
|
test('foobar', async ({}) => {
|
|
|
|
|
const e = new Error('Hello');
|
|
|
|
|
delete e.stack;
|
|
|
|
|
throw e;
|
|
|
|
|
});
|
|
|
|
|
`
|
|
|
|
|
});
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
|
expect(result.failed).toBe(1);
|
|
|
|
|
expect(result.output).toContain('Hello');
|
|
|
|
|
});
|
2021-09-16 07:28:36 +03:00
|
|
|
|
|
|
|
|
|
test('should print errors with inconsistent message/stack', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'a.spec.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-09-16 07:28:36 +03:00
|
|
|
|
test('foobar', async function myTest({}) {
|
|
|
|
|
const e = new Error('Hello');
|
|
|
|
|
// Force stack to contain "Hello".
|
|
|
|
|
// Otherwise it is computed lazy and will get 'foo bar' instead.
|
|
|
|
|
e.stack;
|
|
|
|
|
e.message = 'foo bar';
|
|
|
|
|
throw e;
|
|
|
|
|
});
|
|
|
|
|
`
|
|
|
|
|
});
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
|
expect(result.failed).toBe(1);
|
2023-02-08 02:11:44 +03:00
|
|
|
|
const output = result.output;
|
2023-02-22 01:15:11 +03:00
|
|
|
|
expect(output).toContain('foo bar');
|
2022-07-14 02:11:38 +03:00
|
|
|
|
expect(output).toContain('function myTest');
|
2021-09-16 07:28:36 +03:00
|
|
|
|
});
|
2021-11-12 03:48:08 +03:00
|
|
|
|
|
|
|
|
|
test('should print "no tests found" error', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({ });
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
2023-01-26 04:26:30 +03:00
|
|
|
|
expect(result.output).toContain('No tests found');
|
2021-11-12 03:48:08 +03:00
|
|
|
|
});
|
2022-02-10 23:33:38 +03:00
|
|
|
|
|
|
|
|
|
test('should not crash on undefined body with manual attachments', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'a.test.js': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2022-02-10 23:33:38 +03:00
|
|
|
|
test('one', async ({}, testInfo) => {
|
|
|
|
|
testInfo.attachments.push({
|
|
|
|
|
name: 'foo.txt',
|
|
|
|
|
body: undefined,
|
|
|
|
|
contentType: 'text/plain'
|
|
|
|
|
});
|
|
|
|
|
expect(1).toBe(2);
|
|
|
|
|
});
|
|
|
|
|
`,
|
|
|
|
|
});
|
2023-02-08 02:11:44 +03:00
|
|
|
|
expect(result.output).not.toContain('Error in reporter');
|
2022-02-10 23:33:38 +03:00
|
|
|
|
expect(result.failed).toBe(1);
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
|
});
|
2022-07-29 00:46:21 +03:00
|
|
|
|
|
|
|
|
|
test('should report fatal errors at the end', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'a.spec.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
|
const test = base.extend({
|
2022-07-29 00:46:21 +03:00
|
|
|
|
fixture: [async ({ }, use) => {
|
|
|
|
|
await use();
|
|
|
|
|
throw new Error('oh my!');
|
|
|
|
|
}, { scope: 'worker' }],
|
|
|
|
|
});
|
|
|
|
|
test('good', async ({ fixture }) => {
|
|
|
|
|
});
|
|
|
|
|
`,
|
|
|
|
|
'b.spec.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
|
const test = base.extend({
|
2022-07-29 00:46:21 +03:00
|
|
|
|
fixture: [async ({ }, use) => {
|
|
|
|
|
await use();
|
|
|
|
|
throw new Error('oh my!');
|
|
|
|
|
}, { scope: 'worker' }],
|
|
|
|
|
});
|
|
|
|
|
test('good', async ({ fixture }) => {
|
|
|
|
|
});
|
|
|
|
|
`,
|
|
|
|
|
}, { reporter: 'list' });
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
|
expect(result.passed).toBe(2);
|
2023-02-08 02:11:44 +03:00
|
|
|
|
expect(result.output).toContain('2 errors were not a part of any test, see above for details');
|
2022-07-29 00:46:21 +03:00
|
|
|
|
});
|
2022-12-23 02:57:55 +03:00
|
|
|
|
|
|
|
|
|
test('should contain at most 1 decimal for humanized timing', async ({ runInlineTest }) => {
|
|
|
|
|
const result = await runInlineTest({
|
|
|
|
|
'a.spec.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
2022-12-23 02:57:55 +03:00
|
|
|
|
test('should work', () => {});
|
|
|
|
|
`
|
|
|
|
|
});
|
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
|
expect(result.passed).toBe(1);
|
2023-02-08 02:11:44 +03:00
|
|
|
|
expect(result.output).toMatch(/\d+ passed \(\d+(\.\d)?(ms|s)\)/);
|
2022-12-23 02:57:55 +03:00
|
|
|
|
});
|