2021-06-23 20:30:54 +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.
|
|
|
|
*/
|
|
|
|
|
2022-02-02 05:40:44 +03:00
|
|
|
import { test, expect, stripAnsi } from './playwright-test-fixtures';
|
|
|
|
import path from 'path';
|
2021-06-23 20:30:54 +03:00
|
|
|
|
|
|
|
test('should return the location of a syntax error', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'error.spec.js': `
|
|
|
|
const x = {
|
|
|
|
foo: 'bar';
|
|
|
|
};
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(result.passed).toBe(0);
|
|
|
|
expect(result.failed).toBe(0);
|
|
|
|
expect(result.output).toContain('error.spec.js:6');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should print an improper error', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'error.spec.js': `
|
|
|
|
throw 123;
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(result.passed).toBe(0);
|
|
|
|
expect(result.failed).toBe(0);
|
|
|
|
expect(result.output).toContain('123');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should print a null error', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'error.spec.js': `
|
|
|
|
throw null;
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(result.passed).toBe(0);
|
|
|
|
expect(result.failed).toBe(0);
|
|
|
|
expect(result.output).toContain('null');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return the location of a syntax error in typescript', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'error.spec.ts': `
|
|
|
|
const x = {
|
|
|
|
foo: 'bar';
|
|
|
|
};
|
|
|
|
`
|
|
|
|
}, {}, {
|
|
|
|
FORCE_COLOR: '0'
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(result.passed).toBe(0);
|
|
|
|
expect(result.failed).toBe(0);
|
|
|
|
expect(result.output).toContain('error.spec.ts');
|
|
|
|
expect(result.output).toContain(`'bar';`);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should allow export default form the config file', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'playwright.config.ts': `
|
|
|
|
export default { timeout: 1000 };
|
|
|
|
`,
|
|
|
|
'a.test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('fails', async ({}, testInfo) => {
|
|
|
|
await new Promise(f => setTimeout(f, 2000));
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(result.failed).toBe(1);
|
|
|
|
expect(result.output).toContain('Timeout of 1000ms exceeded.');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should validate configuration object', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'playwright.config.ts': `
|
|
|
|
export default { timeout: '1000' };
|
|
|
|
`,
|
|
|
|
'a.test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('works', () => {});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(result.failed).toBe(0);
|
|
|
|
expect(result.output).toContain('playwright.config.ts: config.timeout must be a non-negative number');
|
|
|
|
});
|
2021-06-29 21:49:50 +03:00
|
|
|
|
|
|
|
test('should match tests well', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('works', () => {});
|
|
|
|
`,
|
|
|
|
'hello.spec.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('works', () => {});
|
|
|
|
`,
|
|
|
|
'test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('works', () => {});
|
|
|
|
`,
|
|
|
|
'spec.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('works', () => {});
|
|
|
|
`,
|
|
|
|
'strange.....spec.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('works', () => {});
|
|
|
|
`,
|
|
|
|
'badspec.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('bad', () => { throw new Error('badspec.ts')});
|
|
|
|
`,
|
|
|
|
'specspec.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('bad', () => { throw new Error('specspec.ts')});
|
|
|
|
`,
|
|
|
|
'a.testtest.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('bad', () => { throw new Error('a.testtest.ts')});
|
|
|
|
`,
|
|
|
|
'b.testspec.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('bad', () => { throw new Error('b.testspec.ts')});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
expect(result.passed).toBe(5);
|
|
|
|
});
|
2021-06-30 01:28:41 +03:00
|
|
|
|
|
|
|
test('should load an mjs file', async ({ runInlineTest }) => {
|
|
|
|
const { exitCode, passed } = await runInlineTest({
|
|
|
|
'a.spec.mjs': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('succeeds', () => {
|
|
|
|
expect(1 + 1).toBe(2);
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(passed).toBe(1);
|
|
|
|
expect(exitCode).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should throw a nice error if a js file uses import', async ({ runInlineTest }) => {
|
|
|
|
const { exitCode, output } = await runInlineTest({
|
|
|
|
'a.spec.js': `
|
|
|
|
import fs from 'fs';
|
|
|
|
const { test } = folio;
|
|
|
|
test('succeeds', () => {
|
|
|
|
expect(1 + 1).toBe(2);
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(exitCode).toBe(1);
|
|
|
|
expect(output).toContain('a.spec.js');
|
|
|
|
expect(output).toContain('JavaScript files must end with .mjs to use import.');
|
|
|
|
});
|
2021-07-12 19:59:58 +03:00
|
|
|
|
|
|
|
test('should load esm when package.json has type module', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'playwright.config.js': `
|
|
|
|
//@no-header
|
|
|
|
import * as fs from 'fs';
|
|
|
|
export default { projects: [{name: 'foo'}] };
|
|
|
|
`,
|
2021-09-27 19:58:08 +03:00
|
|
|
'package.json': JSON.stringify({ type: 'module' }),
|
2021-11-24 23:42:48 +03:00
|
|
|
'a.esm.test.js': `
|
2021-07-12 19:59:58 +03:00
|
|
|
const { test } = pwt;
|
|
|
|
test('check project name', ({}, testInfo) => {
|
|
|
|
expect(testInfo.project.name).toBe('foo');
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
expect(result.passed).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should load esm config files', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'playwright.config.mjs': `
|
|
|
|
//@no-header
|
|
|
|
import * as fs from 'fs';
|
|
|
|
export default { projects: [{name: 'foo'}] };
|
|
|
|
`,
|
|
|
|
'a.test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('check project name', ({}, testInfo) => {
|
|
|
|
expect(testInfo.project.name).toBe('foo');
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
expect(result.passed).toBe(1);
|
|
|
|
});
|
2021-11-09 00:25:40 +03:00
|
|
|
|
|
|
|
test('should fail to load ts from esm when package.json has type module', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'playwright.config.js': `
|
|
|
|
//@no-header
|
|
|
|
import * as fs from 'fs';
|
|
|
|
export default { projects: [{name: 'foo'}] };
|
|
|
|
`,
|
|
|
|
'package.json': JSON.stringify({ type: 'module' }),
|
|
|
|
'a.test.js': `
|
|
|
|
import { foo } from './b.ts';
|
|
|
|
const { test } = pwt;
|
|
|
|
test('check project name', ({}, testInfo) => {
|
|
|
|
expect(testInfo.project.name).toBe('foo');
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
'b.ts': `
|
|
|
|
export const foo: string = 'foo';
|
|
|
|
`
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(result.output).toContain('Cannot import a typescript file from an esmodule');
|
|
|
|
});
|
2021-11-24 23:42:48 +03:00
|
|
|
|
|
|
|
test('should import esm from ts when package.json has type module in experimental mode', async ({ runInlineTest }) => {
|
|
|
|
// We only support experimental esm mode on Node 16+
|
|
|
|
test.skip(parseInt(process.version.slice(1), 10) < 16);
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'playwright.config.ts': `
|
|
|
|
import * as fs from 'fs';
|
|
|
|
export default { projects: [{name: 'foo'}] };
|
|
|
|
`,
|
|
|
|
'package.json': JSON.stringify({ type: 'module' }),
|
|
|
|
'a.test.ts': `
|
|
|
|
import { foo } from './b.ts';
|
|
|
|
const { test } = pwt;
|
|
|
|
test('check project name', ({}, testInfo) => {
|
|
|
|
expect(testInfo.project.name).toBe('foo');
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
'b.ts': `
|
|
|
|
export const foo: string = 'foo';
|
|
|
|
`
|
|
|
|
}, {}, {
|
2021-11-25 01:17:01 +03:00
|
|
|
PW_EXPERIMENTAL_TS_ESM: true
|
2021-11-24 23:42:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
});
|
2022-02-02 05:40:44 +03:00
|
|
|
|
|
|
|
test('should filter stack trace for simple expect', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'expect-test.spec.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('should work', () => {
|
|
|
|
test.expect(1+1).toEqual(3);
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(stripAnsi(result.output)).not.toContain(path.sep + `playwright-test`);
|
|
|
|
expect(stripAnsi(result.output)).not.toContain(path.sep + `playwright-core`);
|
|
|
|
expect(stripAnsi(result.output)).not.toContain('internal');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should filter stack trace for web-first assertions', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'expect-test.spec.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('should work', async ({page}) => {
|
|
|
|
await expect(page.locator('x-foo'), 'x-foo must be visible').toBeVisible({timeout: 1});
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(stripAnsi(result.output)).not.toContain(path.sep + `playwright-test`);
|
|
|
|
expect(stripAnsi(result.output)).not.toContain(path.sep + `playwright-core`);
|
|
|
|
expect(stripAnsi(result.output)).not.toContain('internal');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should filter out event emitter from stack traces', async ({ runInlineTest }, testInfo) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'expect-test.spec.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
const EventEmitter = require('events');
|
|
|
|
test('should work', async ({}) => {
|
|
|
|
const emitter = new EventEmitter();
|
|
|
|
emitter.on('event', function handle() { expect(1).toBe(2); });
|
|
|
|
emitter.emit('event');
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
const outputWithoutGoodStackFrames = stripAnsi(result.output).split('\n').filter(line => !line.includes(testInfo.outputPath())).join('\n');
|
|
|
|
expect(outputWithoutGoodStackFrames).not.toContain('EventEmitter.emit');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should filter stack trace for raw errors', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'expect-test.spec.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('should work', async ({}) => {
|
|
|
|
throw new Error('foobar!');
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(stripAnsi(result.output)).toContain('foobar!');
|
|
|
|
expect(stripAnsi(result.output)).not.toContain(path.sep + `playwright-test`);
|
|
|
|
expect(stripAnsi(result.output)).not.toContain(path.sep + `playwright-core`);
|
|
|
|
expect(stripAnsi(result.output)).not.toContain('internal');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not filter out POM', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'helper.ts': `
|
|
|
|
export function foo() {
|
|
|
|
throw new Error('foo');
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
'expect-test.spec.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
const { foo } = require('./helper');
|
|
|
|
test('should work', ({}) => {
|
|
|
|
foo();
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(stripAnsi(result.output)).toContain('foo');
|
|
|
|
expect(stripAnsi(result.output)).toContain('helper.ts');
|
|
|
|
expect(stripAnsi(result.output)).toContain('expect-test.spec.ts');
|
|
|
|
expect(stripAnsi(result.output)).not.toContain(path.sep + `playwright-test`);
|
|
|
|
expect(stripAnsi(result.output)).not.toContain(path.sep + `playwright-core`);
|
|
|
|
expect(stripAnsi(result.output)).not.toContain('internal');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should filter stack even without default Error.prepareStackTrace', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'expect-test.spec.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
test('should work', ({}) => {
|
|
|
|
Error.prepareStackTrace = undefined;
|
|
|
|
throw new Error('foobar');
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(stripAnsi(result.output)).toContain('foobar');
|
|
|
|
expect(stripAnsi(result.output)).toContain('expect-test.spec.ts');
|
|
|
|
expect(stripAnsi(result.output)).not.toContain(path.sep + `playwright-test`);
|
|
|
|
expect(stripAnsi(result.output)).not.toContain(path.sep + `playwright-core`);
|
|
|
|
expect(stripAnsi(result.output)).not.toContain('internal');
|
|
|
|
const stackLines = stripAnsi(result.output).split('\n').filter(line => line.includes(' at '));
|
|
|
|
expect(stackLines.length).toBe(1);
|
|
|
|
});
|
|
|
|
|