fix(html): always highlight error code (#9604)

This commit is contained in:
Pavel Feldman 2021-10-18 20:37:19 -08:00 committed by GitHub
parent e37660b068
commit c06a6e1f63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 9 deletions

View file

@ -98,7 +98,7 @@ export class BaseReporter implements Reporter {
} }
onError(error: TestError) { onError(error: TestError) {
console.log(formatError(error).message); console.log(formatError(error, colors.enabled));
} }
async onEnd(result: FullResult) { async onEnd(result: FullResult) {
@ -219,7 +219,7 @@ export function formatFailure(config: FullConfig, test: TestCase, options: {inde
lines.push(colors.red(header)); lines.push(colors.red(header));
for (const result of test.results) { for (const result of test.results) {
const resultLines: string[] = []; const resultLines: string[] = [];
const { tokens: resultTokens, position } = formatResultFailure(test, result, ' '); const { tokens: resultTokens, position } = formatResultFailure(test, result, ' ', colors.enabled);
if (!resultTokens.length) if (!resultTokens.length)
continue; continue;
if (result.retry) { if (result.retry) {
@ -281,7 +281,7 @@ export function formatFailure(config: FullConfig, test: TestCase, options: {inde
}; };
} }
export function formatResultFailure(test: TestCase, result: TestResult, initialIndent: string): FailureDetails { export function formatResultFailure(test: TestCase, result: TestResult, initialIndent: string, highlightCode: boolean): FailureDetails {
const resultTokens: string[] = []; const resultTokens: string[] = [];
if (result.status === 'timedOut') { if (result.status === 'timedOut') {
resultTokens.push(''); resultTokens.push('');
@ -293,7 +293,7 @@ export function formatResultFailure(test: TestCase, result: TestResult, initialI
} }
let error: ErrorDetails | undefined = undefined; let error: ErrorDetails | undefined = undefined;
if (result.error !== undefined) { if (result.error !== undefined) {
error = formatError(result.error, test.location.file); error = formatError(result.error, highlightCode, test.location.file);
resultTokens.push(indent(error.message, initialIndent)); resultTokens.push(indent(error.message, initialIndent));
} }
return { return {
@ -325,7 +325,7 @@ function formatTestHeader(config: FullConfig, test: TestCase, indent: string, in
return pad(header, '='); return pad(header, '=');
} }
function formatError(error: TestError, file?: string): ErrorDetails { export function formatError(error: TestError, highlightCode: boolean, file?: string): ErrorDetails {
const stack = error.stack; const stack = error.stack;
const tokens = ['']; const tokens = [''];
let positionInFile: PositionInFile | undefined; let positionInFile: PositionInFile | undefined;
@ -337,7 +337,7 @@ function formatError(error: TestError, file?: string): ErrorDetails {
positionInFile = position; positionInFile = position;
tokens.push(message); tokens.push(message);
const codeFrame = generateCodeFrame(file, position); const codeFrame = generateCodeFrame(highlightCode, file, position);
if (codeFrame) { if (codeFrame) {
tokens.push(''); tokens.push('');
tokens.push(codeFrame); tokens.push(codeFrame);
@ -365,7 +365,7 @@ function indent(lines: string, tab: string) {
return lines.replace(/^(?=.+$)/gm, tab); return lines.replace(/^(?=.+$)/gm, tab);
} }
function generateCodeFrame(file?: string, position?: PositionInFile): string | undefined { function generateCodeFrame(highlightCode: boolean, file?: string, position?: PositionInFile): string | undefined {
if (!position || !file) if (!position || !file)
return; return;
@ -373,7 +373,7 @@ function generateCodeFrame(file?: string, position?: PositionInFile): string | u
const codeFrame = codeFrameColumns( const codeFrame = codeFrameColumns(
source, source,
{ start: position }, { start: position },
{ highlightCode: colors.enabled } { highlightCode }
); );
return codeFrame; return codeFrame;

View file

@ -188,7 +188,7 @@ class RawReporter {
startTime: result.startTime.toISOString(), startTime: result.startTime.toISOString(),
duration: result.duration, duration: result.duration,
status: result.status, status: result.status,
error: formatResultFailure(test, result, '').tokens.join('').trim(), error: formatResultFailure(test, result, '', true).tokens.join('').trim(),
attachments: this._createAttachments(result), attachments: this._createAttachments(result),
steps: this._serializeSteps(test, result.steps) steps: this._serializeSteps(test, result.steps)
}; };

View file

@ -246,3 +246,20 @@ test('should include stdio', async ({ runInlineTest, page, showReport }) => {
await page.locator('text=stderr').click(); await page.locator('text=stderr').click();
await expect(page.locator('.attachment-body').nth(1)).toHaveText('Third line'); await expect(page.locator('.attachment-body').nth(1)).toHaveText('Third line');
}); });
test('should highlight error', async ({ runInlineTest, page, showReport }) => {
const result = await runInlineTest({
'a.test.js': `
const { test } = pwt;
test('fails', async ({ page }) => {
await expect(true).toBeFalsy();
});
`,
}, { reporter: 'dot,html' });
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
await showReport();
await page.click('text=fails');
await expect(page.locator('.error-message span:has-text("received")').nth(1)).toHaveCSS('color', 'rgb(204, 0, 0)');
});