Skip to content

Commit

Permalink
#1030@trivial: Continues on implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Sep 6, 2023
1 parent 9c2b136 commit 7d4750e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
8 changes: 4 additions & 4 deletions packages/happy-dom/src/console/VirtualConsoleLogLevelEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* @see https://console.spec.whatwg.org/#loglevel-severity
*/
enum VirtualConsoleLogLevelEnum {
error = 0,
warn = 1,
info = 2,
log = 3
log = 0,
info = 1,
warn = 2,
error = 3
}
export default VirtualConsoleLogLevelEnum;
4 changes: 2 additions & 2 deletions packages/happy-dom/src/console/VirtualConsoleUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export default class VirtualConsoleUtility {
public static stringifyMessage(message: Array<string | object>): string {
let output = '';
for (const part of message) {
if (typeof part === 'object') {
if (typeof part === 'object' && (part === null || part.constructor.name === 'Object')) {
try {
output += JSON.stringify(part, null, 3);
} catch (error) {
output += '["Failed stringify object in log entry."]';
}
} else {
output += part;
output += String(part);
}
}
return output;
Expand Down
6 changes: 4 additions & 2 deletions packages/happy-dom/src/window/WindowErrorUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ export default class WindowErrorUtility {
*/
public static dispatchError(elementOrWindow: IWindow | IElement, error: Error): void {
if ((<IWindow>elementOrWindow).console) {
(<IWindow>elementOrWindow).console.error(error);
(<IWindow>elementOrWindow).console.error(error.message + '\n' + error.stack);
elementOrWindow.dispatchEvent(new ErrorEvent('error', { message: error.message, error }));
} else {
(<IElement>elementOrWindow).ownerDocument.defaultView.console.error(error);
(<IElement>elementOrWindow).ownerDocument.defaultView.console.error(
error.message + '\n' + error.stack
);
(<IElement>elementOrWindow).dispatchEvent(
new ErrorEvent('error', { message: error.message, error })
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@ export default class UncaughtExceptionObserver {
return;
}

if (
error instanceof this.window.Error &&
error.stack?.includes('at main (eval') &&
error.stack?.includes('/happy-dom/')
) {
this.window.console.error(error);
if (error instanceof this.window.Error && error.stack?.includes('/happy-dom/')) {
this.window.console.error(error.message + '\n' + error.stack);
this.window.dispatchEvent(new ErrorEvent('error', { error, message: error.message }));
} else if (
process.listenerCount('uncaughtException') ===
(<typeof UncaughtExceptionObserver>this.constructor).listenerCount
) {
// eslint-disable-next-line no-console
console.error(error.message + '\n' + error.stack);
// Exit if there are no other listeners handling the error.
process.exit(1);
}
Expand All @@ -53,17 +51,15 @@ export default class UncaughtExceptionObserver {
// The "uncaughtException" event is not always triggered for unhandled rejections.
// Therefore we want to use the "unhandledRejection" event as well.
this.uncaughtRejectionListener = (error: Error) => {
if (
error instanceof this.window.Error &&
error.stack?.includes('at main (eval') &&
error.stack?.includes('/happy-dom/')
) {
this.window.console.error(error);
if (error instanceof this.window.Error && error.stack?.includes('/happy-dom/')) {
this.window.console.error(error.message + '\n' + error.stack);
this.window.dispatchEvent(new ErrorEvent('error', { error, message: error.message }));
} else if (
process.listenerCount('unhandledRejection') ===
(<typeof UncaughtExceptionObserver>this.constructor).listenerCount
) {
// eslint-disable-next-line no-console
console.error(error.message + '\n' + error.stack);
// Exit if there are no other listeners handling the error.
process.exit(1);
}
Expand All @@ -77,6 +73,10 @@ export default class UncaughtExceptionObserver {
* Disconnects observer.
*/
public disconnect(): void {
if (!this.window) {
return;
}

(<typeof UncaughtExceptionObserver>this.constructor).listenerCount--;

process.off('uncaughtException', this.uncaughtExceptionListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ async function itObservesUncaughtExceptions(): Promise<void> {

window.addEventListener('error', (event) => (errorEvent = <ErrorEvent>event));

window['customSetTimeout'] = setTimeout;
window['customSetTimeout'] = setTimeout.bind(globalThis);

document.write(`
<script>
(() => {
function main() {
customSetTimeout(() => {
throw new Error('Test error');
});
}, 0);
}
main();
Expand All @@ -75,8 +75,17 @@ async function itObservesUncaughtExceptions(): Promise<void> {

observer.disconnect();

const actualConsoleOutput = window.happyDOM.virtualConsolePrinter.readAsString();
const expectedConsoleOutput = ``;
const actualConsoleOutput = window.happyDOM.virtualConsolePrinter
.readAsString()
.replace(/[\s0-9]|\(file:.+\/happy-dom\//gm, '');
const expectedConsoleOutput = `Test error
Error: Test error
at Timeout.eval [as _onTimeout] (eval at <anonymous> (file:///home/user/happy-dom/packages/happy-dom/lib/nodes/html-script-element/HTMLScriptElement.js:171:126), <anonymous>:5:31)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7)`.replace(
/[\s0-9]|\(file:.+\/happy-dom\//gm,
''
);

if (actualConsoleOutput !== expectedConsoleOutput) {
throw new Error(`Console output not correct.`);
Expand All @@ -95,5 +104,15 @@ async function itObservesUncaughtExceptions(): Promise<void> {
}
}

itObservesUnhandledRejections();
itObservesUncaughtExceptions();
async function main(): Promise<void> {
try {
await itObservesUnhandledRejections();
await itObservesUncaughtExceptions();
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
process.exit(1);
}
}

main();

0 comments on commit 7d4750e

Please sign in to comment.