fix(reporters): carefully handle empty lines (#28591)

In some circumstances, like "No tests found" error, reporters produce a
lot of unnecessary empty lines:

```
$ npx playwright test
Error: No tests found




$ <next command>
```

Also, `line` reporter removes the `npx playwright test` command entirely
when `onError` happens before `onBegin`.
This commit is contained in:
Dmitry Gozman 2023-12-11 21:18:48 -08:00 committed by GitHub
parent d20a20b9b6
commit 10dda30c7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -470,7 +470,7 @@ export function formatError(error: TestError, highlightCode: boolean): ErrorDeta
tokens.push(snippet); tokens.push(snippet);
} }
if (parsedStack) { if (parsedStack && parsedStack.stackLines.length) {
tokens.push(''); tokens.push('');
tokens.push(colors.dim(parsedStack.stackLines.join('\n'))); tokens.push(colors.dim(parsedStack.stackLines.join('\n')));
} }

View File

@ -21,6 +21,7 @@ class LineReporter extends BaseReporter {
private _current = 0; private _current = 0;
private _failures = 0; private _failures = 0;
private _lastTest: TestCase | undefined; private _lastTest: TestCase | undefined;
private _didBegin = false;
override printsToStdio() { override printsToStdio() {
return true; return true;
@ -28,8 +29,12 @@ class LineReporter extends BaseReporter {
override onBegin(suite: Suite) { override onBegin(suite: Suite) {
super.onBegin(suite); super.onBegin(suite);
console.log(this.generateStartingMessage()); const startingMessage = this.generateStartingMessage();
console.log(); if (startingMessage) {
console.log(startingMessage);
console.log();
}
this._didBegin = true;
} }
override onStdOut(chunk: string | Buffer, test?: TestCase, result?: TestResult) { override onStdOut(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
@ -105,15 +110,15 @@ class LineReporter extends BaseReporter {
override onError(error: TestError): void { override onError(error: TestError): void {
super.onError(error); super.onError(error);
const message = formatError(error, colors.enabled).message + '\n\n'; const message = formatError(error, colors.enabled).message + '\n';
if (!process.env.PW_TEST_DEBUG_REPORTERS) if (!process.env.PW_TEST_DEBUG_REPORTERS && this._didBegin)
process.stdout.write(`\u001B[1A\u001B[2K`); process.stdout.write(`\u001B[1A\u001B[2K`);
process.stdout.write(message); process.stdout.write(message);
console.log(); console.log();
} }
override async onEnd(result: FullResult) { override async onEnd(result: FullResult) {
if (!process.env.PW_TEST_DEBUG_REPORTERS) if (!process.env.PW_TEST_DEBUG_REPORTERS && this._didBegin)
process.stdout.write(`\u001B[1A\u001B[2K`); process.stdout.write(`\u001B[1A\u001B[2K`);
await super.onEnd(result); await super.onEnd(result);
this.epilogue(false); this.epilogue(false);