Skip to content

Commit

Permalink
feat: add support unhandledrejection show as error
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-redFox committed Oct 23, 2023
1 parent 86a4c98 commit bd9d82c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
5 changes: 5 additions & 0 deletions client/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ function Karma (updater, socket, iframe, opener, navigator, location, document)
if (error && error.stack) {
message += '\n\n' + error.stack
}
} else if (messageOrEvent?.type === 'unhandledrejection') {
message = messageOrEvent.reason.message
if (messageOrEvent.reason.stack) {
message += '\n\n' + messageOrEvent.reason.stack
}
} else {
// create an object with the string representation of the message to
// ensure all its content is properly transferred to the console log
Expand Down
2 changes: 2 additions & 0 deletions context/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class ContextKarma {
return this.error(...args)
}

contextWindow.onunhandledrejection = (event) => this.error(event)

contextWindow.onbeforeunload = () => {
return this.error('Some of your tests did a full page reload!')
}
Expand Down
17 changes: 10 additions & 7 deletions static/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
};
} (util));

var instanceOf = util.instanceOf;
const instanceOf = util.instanceOf;

function isNode (obj) {
return (obj.tagName || obj.nodeName) && obj.nodeType
Expand Down Expand Up @@ -64,11 +64,11 @@
}
case 'boolean':
return obj ? 'true' : 'false'
case 'object':
var strs = [];
case 'object': {
const strs = [];
if (instanceOf(obj, 'Array')) {
strs.push('[');
for (var i = 0, ii = obj.length; i < ii; i++) {
for (let i = 0, ii = obj.length; i < ii; i++) {
if (i) {
strs.push(', ');
}
Expand All @@ -88,15 +88,15 @@
} else if (instanceOf(obj, 'Error')) {
return obj.toString() + '\n' + obj.stack
} else {
var constructor = 'Object';
let constructor = 'Object';
if (obj.constructor && typeof obj.constructor === 'function') {
constructor = obj.constructor.name;
}

strs.push(constructor);
strs.push('{');
var first = true;
for (var key in obj) {
let first = true;
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (first) {
first = false;
Expand All @@ -110,6 +110,7 @@
strs.push('}');
}
return strs.join('')
}
default:
return obj
}
Expand Down Expand Up @@ -193,6 +194,8 @@
return this.error(...args)
};

contextWindow.onunhandledrejection = (event) => this.error(event);

contextWindow.onbeforeunload = () => {
return this.error('Some of your tests did a full page reload!')
};
Expand Down
20 changes: 13 additions & 7 deletions static/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
};
} (util$2));

var instanceOf = util$2.instanceOf;
const instanceOf = util$2.instanceOf;

function isNode (obj) {
return (obj.tagName || obj.nodeName) && obj.nodeType
Expand Down Expand Up @@ -64,11 +64,11 @@
}
case 'boolean':
return obj ? 'true' : 'false'
case 'object':
var strs = [];
case 'object': {
const strs = [];
if (instanceOf(obj, 'Array')) {
strs.push('[');
for (var i = 0, ii = obj.length; i < ii; i++) {
for (let i = 0, ii = obj.length; i < ii; i++) {
if (i) {
strs.push(', ');
}
Expand All @@ -88,15 +88,15 @@
} else if (instanceOf(obj, 'Error')) {
return obj.toString() + '\n' + obj.stack
} else {
var constructor = 'Object';
let constructor = 'Object';
if (obj.constructor && typeof obj.constructor === 'function') {
constructor = obj.constructor.name;
}

strs.push(constructor);
strs.push('{');
var first = true;
for (var key in obj) {
let first = true;
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (first) {
first = false;
Expand All @@ -110,6 +110,7 @@
strs.push('}');
}
return strs.join('')
}
default:
return obj
}
Expand Down Expand Up @@ -297,6 +298,11 @@
if (error && error.stack) {
message += '\n\n' + error.stack;
}
} else if (messageOrEvent?.type === 'unhandledrejection') {
message = messageOrEvent.reason.message;
if (messageOrEvent.reason.stack) {
message += '\n\n' + messageOrEvent.reason.stack;
}
} else {
// create an object with the string representation of the message to
// ensure all its content is properly transferred to the console log
Expand Down

0 comments on commit bd9d82c

Please sign in to comment.