Skip to content

Commit 0ab8430

Browse files
authored
fix: improve error message when we failed to read playwright config (#722)
1 parent cda5bc7 commit 0ab8430

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

lib/adapters/tool/playwright/index.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ async function readPwtConfig(opts: ToolAdapterOptionsFromCli): Promise<{configPa
227227
const configPaths = opts.configPath ? [opts.configPath] : DEFAULT_CONFIG_PATHS;
228228
let originalConfig!: FullConfig;
229229
let resolvedConfigPath!: string;
230+
const errors: Array<{path: string, error: Error}> = [];
230231

231232
const revertTransformHook = setupTransformHook();
232233

@@ -239,7 +240,11 @@ async function readPwtConfig(opts: ToolAdapterOptionsFromCli): Promise<{configPa
239240

240241
break;
241242
} catch (err) {
243+
const error = err as Error;
244+
errors.push({path: resolvedConfigPath, error});
245+
242246
if ((err as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') {
247+
revertTransformHook();
243248
throw err;
244249
}
245250
}
@@ -248,12 +253,43 @@ async function readPwtConfig(opts: ToolAdapterOptionsFromCli): Promise<{configPa
248253
revertTransformHook();
249254

250255
if (!originalConfig) {
251-
throw new Error(`Unable to read config from paths: ${configPaths.join(', ')}`);
256+
const errorMessage = createConfigReadErrorMessage(configPaths, errors);
257+
const error = new Error('');
258+
error.stack = errorMessage;
259+
throw error;
252260
}
253261

254262
return {config: originalConfig, configPath: resolvedConfigPath};
255263
}
256264

265+
function createConfigReadErrorMessage(searchedPaths: string[], errors: Array<{path: string, error: Error}>): string {
266+
const lines: string[] = [];
267+
268+
lines.push('HTML Reporter failed to read Playwright configuration file.\n');
269+
lines.push('What happened:');
270+
271+
if (searchedPaths.length === 0) {
272+
lines.push(` • No config paths were supplied`);
273+
}
274+
275+
errors.forEach(({path, error}) => {
276+
if (new RegExp('^Cannot find module[ \'"]*' + path).test(error.message)) {
277+
lines.push(` • Tried to read ${path}, but got an error: file does not exist`);
278+
} else {
279+
lines.push(` • Tried to read ${path}, but got an error: ${error.message.replaceAll('\n', '\n ')}`);
280+
}
281+
});
282+
283+
lines.push('\nWhat you can do:');
284+
lines.push(' • Ensure playwright config file exists in one of those locations');
285+
lines.push(' • Specify correct path using --config option');
286+
lines.push(' • Ensure playwright config file is valid');
287+
lines.push(' • If you are using path alises like "@/tests" or "~/tests", ensure they can be resolved at runtime.');
288+
lines.push(' You may need to install tsconfig-paths package. Read more: https://npm.im/tsconfig-paths');
289+
290+
return lines.join('\n');
291+
}
292+
257293
async function registerTestResult(eventMsg: PwtEventMessage, reportBuilder: GuiReportBuilder): Promise<TestBranch> {
258294
const {test, result, browserName, titlePath} = eventMsg;
259295

lib/cli/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ process.on('uncaughtException', err => {
1616
});
1717

1818
process.on('unhandledRejection', (reason, p) => {
19-
const error = new Error([
19+
const errorMessage = [
2020
`Unhandled Rejection in ${toolAdapter ? toolAdapter.toolName + ':' : ''}${process.pid}:`,
2121
`Promise: ${JSON.stringify(p)}`,
2222
`Reason: ${_.get(reason, 'stack', reason)}`
23-
].join('\n'));
23+
].join('\n');
2424

2525
if (toolAdapter) {
26-
toolAdapter.halt(error, 60000);
26+
toolAdapter.halt(new Error(errorMessage), 60000);
2727
} else {
28-
logger.error(error);
28+
logger.error(errorMessage);
2929
process.exit(1);
3030
}
3131
});

test/unit/lib/adapters/tool/playwright/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,14 @@ describe('lib/adapters/tool/playwright/index', () => {
9696
});
9797

9898
it('should throw error if config file is not found', async () => {
99-
await assert.isRejected(
100-
createPwtToolAdapter({toolName: ToolName.Playwright}),
101-
`Unable to read config from paths: ${DEFAULT_CONFIG_PATHS.join(', ')}`
102-
);
99+
let error;
100+
try {
101+
await createPwtToolAdapter({toolName: ToolName.Playwright});
102+
} catch (e) {
103+
error = e;
104+
}
105+
assert.isDefined(error);
106+
assert.include(error.stack, 'HTML Reporter failed to read Playwright configuration file.');
103107
});
104108

105109
describe('parse options from "html-reporter/playwright" reporter', () => {

0 commit comments

Comments
 (0)