Skip to content

Commit

Permalink
feat(test-runner): smaller error message paths (#7384)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelEinbinder authored Jul 19, 2021
1 parent a62aac3 commit d72efbe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
6 changes: 1 addition & 5 deletions src/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

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

Expand Down Expand Up @@ -353,7 +353,3 @@ function errorWithLocations(message: string, ...defined: { location: Location, n
}
return new Error(message);
}

function formatLocation(location: Location) {
return location.file + ':' + location.line + ':' + location.column;
}
12 changes: 6 additions & 6 deletions src/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import type { LaunchOptions, BrowserContextOptions, Page } from '../../types/types';
import type { TestType, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, FullConfig, TestInfo } from '../../types/test';
import type { TestType, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions } from '../../types/test';
import { rootTestType } from './testType';
import { createGuid, removeFolders } from '../utils/utils';
export { expect } from './expect';
Expand Down Expand Up @@ -180,7 +180,7 @@ export const test = _baseTest.extend<PlaywrightTestArgs & PlaywrightTestOptions,
}));
}

const prependToError = testInfo.status === 'timedOut' ? formatPendingCalls((context as any)._connection.pendingProtocolCalls(), testInfo) : '';
const prependToError = testInfo.status === 'timedOut' ? formatPendingCalls((context as any)._connection.pendingProtocolCalls()) : '';
await context.close();
if (prependToError) {
if (!testInfo.error) {
Expand Down Expand Up @@ -215,17 +215,17 @@ export const test = _baseTest.extend<PlaywrightTestArgs & PlaywrightTestOptions,
});
export default test;

function formatPendingCalls(calls: ProtocolCall[], testInfo: TestInfo) {
function formatPendingCalls(calls: ProtocolCall[]) {
if (!calls.length)
return '';
return 'Pending operations:\n' + calls.map(call => {
const frame = call.stack && call.stack[0] ? formatStackFrame(testInfo.config, call.stack[0]) : '<unknown>';
const frame = call.stack && call.stack[0] ? formatStackFrame(call.stack[0]) : '<unknown>';
return ` - ${call.apiName} at ${frame}\n`;
}).join('') + '\n';
}

function formatStackFrame(config: FullConfig, frame: StackFrame) {
const file = path.relative(config.rootDir, frame.file) || path.basename(frame.file);
function formatStackFrame(frame: StackFrame) {
const file = path.relative(process.cwd(), frame.file) || path.basename(frame.file);
return `${file}:${frame.line || 1}:${frame.column || 1}`;
}

Expand Down
6 changes: 1 addition & 5 deletions src/test/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { installTransform } from './transform';
import type { FullConfig, Config, FullProject, Project, ReporterDescription, PreserveOutput } from './types';
import { isRegExp, mergeObjects } from './util';
import { isRegExp, mergeObjects, errorWithFile } from './util';
import { setCurrentlyLoadingFileSuite } from './globals';
import { Suite } from './test';
import { SerializedLoaderData } from './ipc';
Expand Down Expand Up @@ -236,10 +236,6 @@ function toLaunchServers(launchConfigs?: LaunchConfig | LaunchConfig[]): LaunchC
return launchConfigs;
}

function errorWithFile(file: string, message: string) {
return new Error(`${file}: ${message}`);
}

function validateConfig(file: string, config: Config) {
if (typeof config !== 'object' || !config)
throw errorWithFile(file, `Configuration file must export a single object`);
Expand Down
17 changes: 16 additions & 1 deletion src/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/

import util from 'util';
import type { TestError } from './types';
import path from 'path';
import type { TestError, Location } from './types';
import { default as minimatch } from 'minimatch';

export class DeadlineRunner<T> {
Expand Down Expand Up @@ -147,3 +148,17 @@ export function forceRegExp(pattern: string): RegExp {
return new RegExp(match[1], match[2]);
return new RegExp(pattern, 'g');
}

export function relativeFilePath(file: string): string {
if (!path.isAbsolute(file))
return file;
return path.relative(process.cwd(), file);
}

export function formatLocation(location: Location) {
return relativeFilePath(location.file) + ':' + location.line + ':' + location.column;
}

export function errorWithFile(file: string, message: string) {
return new Error(`${relativeFilePath(file)}: ${message}`);
}

0 comments on commit d72efbe

Please sign in to comment.