| // Copyright 2022 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| import {assert} from 'chai'; | |
| import {describe, it} from '../conductor/mocha-interface.js'; | |
| import { | |
| getConsoleMessages, | |
| getStructuredConsoleMessages, | |
| Level, | |
| navigateToConsoleTab, | |
| typeIntoConsoleAndWaitForResult, | |
| } from '../helpers/console-helpers.js'; | |
| import type {DevToolsPage} from '../shared/DevToolsPage.js'; | |
| import type {InspectedPage} from '../shared/InspectedPage.js'; | |
| const FOO_SCRIPT = '\n'.repeat(11) + [ | |
| ' function throwError()', | |
| ' {', | |
| ' throw new Error("error_text");', | |
| ' }', | |
| '', | |
| ' function throwObject()', | |
| ' {', | |
| ' throw {a: 42};', | |
| ' }', | |
| '', | |
| ' function throwNumber()', | |
| ' {', | |
| ' throw 42;', | |
| ' }', | |
| '', | |
| ' function rejectWithError()', | |
| ' {', | |
| ' Promise.reject(new Error("promise_error"));', | |
| ' }', | |
| '', | |
| ' function rejectWithObject()', | |
| ' {', | |
| ' Promise.reject({b: 42});', | |
| ' }', | |
| ' //# sourceURL=foo.js', | |
| ].join('\n'); | |
| async function loadExceptionFunctions(devToolsPage: DevToolsPage, inspectedPage: InspectedPage): Promise<void> { | |
| await inspectedPage.goToResource('empty.html'); | |
| await navigateToConsoleTab(devToolsPage); | |
| await inspectedPage.evaluate(script => { | |
| const el = document.createElement('script'); | |
| el.textContent = script; | |
| document.head.appendChild(el); | |
| }, FOO_SCRIPT); | |
| } | |
| function normalizeVMFrames(text: string|null|undefined): string|null { | |
| return text ? text.replace(/VM\d+:1/g, 'VM:1').trim() : null; | |
| } | |
| async function getFormattedConsoleMessages(devToolsPage: DevToolsPage) { | |
| const messages = await getStructuredConsoleMessages(devToolsPage); | |
| return messages.filter(m => m.message !== undefined).map(m => ({ | |
| message: m.message, | |
| source: m.source, | |
| stackPreview: normalizeVMFrames(m.stackPreview), | |
| })); | |
| } | |
| describe('The Console Tab', () => { | |
| it('Does not crash if it fails to convert alert() argument to string', async ({devToolsPage, inspectedPage}) => { | |
| await navigateToConsoleTab(devToolsPage); | |
| await inspectedPage.reload(); | |
| const result = | |
| (await getConsoleMessages(devToolsPage, inspectedPage, 'alert-toString-exception', undefined, undefined))[0]; | |
| assert.strictEqual(result, 'Uncaught Exception in toString().'); | |
| }); | |
| it('formats synchronous thrown errors, objects, and numbers', async ({devToolsPage, inspectedPage}) => { | |
| await loadExceptionFunctions(devToolsPage, inspectedPage); | |
| await typeIntoConsoleAndWaitForResult(devToolsPage, 'throwError();', 1, Level.Error); | |
| await typeIntoConsoleAndWaitForResult(devToolsPage, 'throwObject();', 1, Level.Error); | |
| await typeIntoConsoleAndWaitForResult(devToolsPage, 'throwNumber();', 1, Level.Error); | |
| assert.deepEqual(await getFormattedConsoleMessages(devToolsPage), [ | |
| { | |
| message: 'Uncaught Error: error_text\n at throwError (foo.js:14:17)\n at <anonymous>:1:1', | |
| source: 'foo.js:14', | |
| stackPreview: 'throwError @ foo.js:14\n(anonymous) @ VM:1', | |
| }, | |
| { | |
| message: 'Uncaught {a: 42}', | |
| source: 'foo.js:19', | |
| stackPreview: 'throwObject @ foo.js:19\n(anonymous) @ VM:1', | |
| }, | |
| { | |
| message: 'Uncaught 42', | |
| source: 'foo.js:24', | |
| stackPreview: 'throwNumber @ foo.js:24\n(anonymous) @ VM:1', | |
| }, | |
| ]); | |
| }); | |
| it('formats setTimeout-scheduled thrown errors, objects, and numbers', async ({devToolsPage, inspectedPage}) => { | |
| await loadExceptionFunctions(devToolsPage, inspectedPage); | |
| await typeIntoConsoleAndWaitForResult(devToolsPage, 'setTimeout(throwError, 0); undefined', 1, Level.Error); | |
| await typeIntoConsoleAndWaitForResult(devToolsPage, 'setTimeout(throwObject, 0); undefined', 1, Level.Error); | |
| await typeIntoConsoleAndWaitForResult(devToolsPage, 'setTimeout(throwNumber, 0); undefined', 1, Level.Error); | |
| assert.deepEqual(await getFormattedConsoleMessages(devToolsPage), [ | |
| { | |
| message: 'undefined', | |
| source: undefined, | |
| stackPreview: null, | |
| }, | |
| { | |
| message: 'Uncaught Error: error_text\n at throwError (foo.js:14:17)', | |
| source: 'foo.js:14', | |
| stackPreview: 'throwError @ foo.js:14\nsetTimeout\n(anonymous) @ VM:1', | |
| }, | |
| { | |
| message: 'undefined', | |
| source: undefined, | |
| stackPreview: null, | |
| }, | |
| { | |
| message: 'Uncaught {a: 42}', | |
| source: 'foo.js:19', | |
| stackPreview: 'throwObject @ foo.js:19\nsetTimeout\n(anonymous) @ VM:1', | |
| }, | |
| { | |
| message: 'undefined', | |
| source: undefined, | |
| stackPreview: null, | |
| }, | |
| { | |
| message: 'Uncaught 42', | |
| source: 'foo.js:24', | |
| stackPreview: 'throwNumber @ foo.js:24\nsetTimeout\n(anonymous) @ VM:1', | |
| }, | |
| ]); | |
| }); | |
| it('formats unhandled promise rejections with error and object values', async ({devToolsPage, inspectedPage}) => { | |
| await loadExceptionFunctions(devToolsPage, inspectedPage); | |
| await typeIntoConsoleAndWaitForResult(devToolsPage, 'setTimeout(rejectWithError, 0); undefined', 1, Level.Error); | |
| await typeIntoConsoleAndWaitForResult(devToolsPage, 'rejectWithError();', 1, Level.Error); | |
| await typeIntoConsoleAndWaitForResult(devToolsPage, 'setTimeout(rejectWithObject, 0); undefined', 1, Level.Error); | |
| await typeIntoConsoleAndWaitForResult(devToolsPage, 'rejectWithObject();', 1, Level.Error); | |
| assert.deepEqual(await getFormattedConsoleMessages(devToolsPage), [ | |
| { | |
| message: 'undefined', | |
| source: undefined, | |
| stackPreview: null, | |
| }, | |
| { | |
| message: 'Uncaught (in promise) Error: promise_error\n at rejectWithError (foo.js:29:26)', | |
| source: 'foo.js:29', | |
| stackPreview: 'rejectWithError @ foo.js:29\nsetTimeout\n(anonymous) @ VM:1', | |
| }, | |
| { | |
| message: 'undefined', | |
| source: undefined, | |
| stackPreview: null, | |
| }, | |
| { | |
| message: | |
| 'Uncaught (in promise) Error: promise_error\n at rejectWithError (foo.js:29:26)\n at <anonymous>:1:1', | |
| source: 'foo.js:29', | |
| stackPreview: 'rejectWithError @ foo.js:29\n(anonymous) @ VM:1', | |
| }, | |
| { | |
| message: 'undefined', | |
| source: undefined, | |
| stackPreview: null, | |
| }, | |
| { | |
| message: 'Uncaught (in promise) {b: 42}', | |
| source: 'foo.js:34', | |
| stackPreview: 'rejectWithObject @ foo.js:34\nsetTimeout\n(anonymous) @ VM:1', | |
| }, | |
| { | |
| message: 'undefined', | |
| source: undefined, | |
| stackPreview: null, | |
| }, | |
| { | |
| message: 'Uncaught (in promise) {b: 42}', | |
| source: 'foo.js:34', | |
| stackPreview: 'rejectWithObject @ foo.js:34\n(anonymous) @ VM:1', | |
| }, | |
| ]); | |
| }); | |
| }); |