Skip to content

Commit b89d9da

Browse files
committed
test: Fix all outstanding integration tests on Chrome/FF/Safari
- Fix MouseEvents related integration tests on Chrome - Fix Mouse/KeyboardEvents related integration tests on Firefox - Update captureException test assertion for Safari - Fix non-error throws in onerror handler on Firefox - Simplify _normalizeFrame edgecase and comment on event factories
1 parent 586ea46 commit b89d9da

File tree

3 files changed

+90
-192
lines changed

3 files changed

+90
-192
lines changed

src/raven.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ Raven.prototype = {
13691369
var frames = [];
13701370
if (stackInfo.stack && stackInfo.stack.length) {
13711371
each(stackInfo.stack, function(i, stack) {
1372-
var frame = self._normalizeFrame(stack);
1372+
var frame = self._normalizeFrame(stack, stackInfo.url);
13731373
if (frame) {
13741374
frames.push(frame);
13751375
}
@@ -1386,9 +1386,7 @@ Raven.prototype = {
13861386
return frames;
13871387
},
13881388

1389-
_normalizeFrame: function(frame) {
1390-
if (!frame.url) return;
1391-
1389+
_normalizeFrame: function(frame, stackInfoUrl) {
13921390
// normalize the frames data
13931391
var normalized = {
13941392
filename: frame.url,
@@ -1397,6 +1395,15 @@ Raven.prototype = {
13971395
function: frame.func || '?'
13981396
};
13991397

1398+
// Case when we don't have any information about the error
1399+
// E.g. throwing a string or raw object, instead of an `Error` in Firefox
1400+
// Generating synthetic error doesn't add any value here
1401+
//
1402+
// We should probably somehow let a user know that they should fix their code
1403+
if (!frame.url) {
1404+
normalized.filename = stackInfoUrl; // fallback to whole stacks url from onerror handler
1405+
}
1406+
14001407
normalized.in_app = !// determine if an exception came from outside of our app
14011408
// first we check the global includePaths list.
14021409
(

test/integration/frame.html

+64
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,70 @@
3737
clearTimeout(id);
3838
};
3939
}());
40+
41+
/**
42+
* Custom event factories for cross-browser compatibility
43+
*
44+
* Gecko browsers are using non-standard `initKeyEvent`, where others implemented `initKeyboardEvent`.
45+
* To make it more consistent, we try to use standardized `MouseEvent`/`KeyboardEvent` now
46+
* and fallback to older implementations for legacy browsers only.
47+
*
48+
* See deprecation notes:
49+
* https://developer.mozilla.org/en-US/docs/Web/API/Document/createEvent
50+
* https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/initKeyEvent
51+
* https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/initKeyboardEvent
52+
*
53+
* References:
54+
* https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent#Specifications
55+
* https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Specifications
56+
*/
57+
function createMouseEvent (options) {
58+
var options = {
59+
bubbles: true,
60+
cancelable: true,
61+
view: window
62+
}
63+
64+
if ('MouseEvent' in window) {
65+
return new MouseEvent('click', options);
66+
} else {
67+
var event = document.createEvent('MouseEvent');
68+
event.initMouseEvent(
69+
'click',
70+
options.bubbles,
71+
options.cancelable,
72+
options.view
73+
);
74+
75+
return event;
76+
}
77+
}
78+
79+
function createKeyboardEvent (key) {
80+
var options = {
81+
bubbles: true,
82+
cancelable: true,
83+
view: window,
84+
key: key || 'a'
85+
}
86+
options.charCode = options.key.charCodeAt();
87+
88+
if ('KeyboardEvent' in window) {
89+
return new KeyboardEvent('keypress', options);
90+
} else {
91+
var event = document.createEvent('KeyboardEvent');
92+
event.initKeyboardEvent(
93+
'keypress',
94+
options.bubbles,
95+
options.cancelable,
96+
options.view,
97+
options.key,
98+
options.charCode
99+
);
100+
101+
return event;
102+
}
103+
}
40104
</script>
41105
<script src="../../node_modules/jquery/dist/jquery.js"></script>
42106
<script src="../../build/raven.js"></script>

test/integration/test.js

+15-188
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('integration', function() {
9797
},
9898
function() {
9999
var ravenData = iframe.contentWindow.ravenData[0];
100-
assert.isAbove(ravenData.stacktrace.frames.length, 1);
100+
assert.isAbove(ravenData.stacktrace.frames.length, 0);
101101

102102
// verify trimHeadFrames hasn't slipped into final payload
103103
assert.isUndefined(ravenData.trimHeadFrames);
@@ -458,14 +458,8 @@ describe('integration', function() {
458458
false
459459
);
460460

461-
var evt;
462-
if (document.createEvent) {
463-
evt = document.createEvent('MouseEvents');
464-
evt.initEvent('click', true, false);
465-
div.dispatchEvent(evt);
466-
} else if (document.createEventObject) {
467-
div.fireEvent('onclick');
468-
}
461+
var click = createMouseEvent();
462+
div.dispatchEvent(click);
469463
},
470464
function() {
471465
var ravenData = iframe.contentWindow.ravenData[0];
@@ -828,24 +822,7 @@ describe('integration', function() {
828822
input.addEventListener('click', clickHandler);
829823

830824
// click <input/>
831-
var evt = document.createEvent('MouseEvent');
832-
evt.initMouseEvent(
833-
'click',
834-
true /* bubble */,
835-
true /* cancelable */,
836-
window,
837-
null,
838-
0,
839-
0,
840-
0,
841-
0 /* coordinates */,
842-
false,
843-
false,
844-
false,
845-
false /* modifier keys */,
846-
0 /*left*/,
847-
null
848-
);
825+
var evt = createMouseEvent();
849826
input.dispatchEvent(evt);
850827
},
851828
function() {
@@ -878,24 +855,7 @@ describe('integration', function() {
878855
Raven._breadcrumbs = [];
879856

880857
// click <input/>
881-
var evt = document.createEvent('MouseEvent');
882-
evt.initMouseEvent(
883-
'click',
884-
true /* bubble */,
885-
true /* cancelable */,
886-
window,
887-
null,
888-
0,
889-
0,
890-
0,
891-
0 /* coordinates */,
892-
false,
893-
false,
894-
false,
895-
false /* modifier keys */,
896-
0 /*left*/,
897-
null
898-
);
858+
var evt = createMouseEvent();
899859

900860
var input = document.getElementsByTagName('input')[0];
901861
input.dispatchEvent(evt);
@@ -941,24 +901,7 @@ describe('integration', function() {
941901
document.querySelector('.c').addEventListener('click', clickHandler);
942902

943903
// click <input/>
944-
var evt = document.createEvent('MouseEvent');
945-
evt.initMouseEvent(
946-
'click',
947-
true /* bubble */,
948-
true /* cancelable */,
949-
window,
950-
null,
951-
0,
952-
0,
953-
0,
954-
0 /* coordinates */,
955-
false,
956-
false,
957-
false,
958-
false /* modifier keys */,
959-
0 /*left*/,
960-
null
961-
);
904+
var evt = createMouseEvent();
962905

963906
var input = document.querySelector('.a'); // leaf node
964907
input.dispatchEvent(evt);
@@ -993,25 +936,7 @@ describe('integration', function() {
993936
Raven._breadcrumbs = [];
994937

995938
// click <input/>
996-
var evt = document.createEvent('MouseEvent');
997-
evt.initMouseEvent(
998-
evt,
999-
'click',
1000-
true /* bubble */,
1001-
true /* cancelable */,
1002-
window,
1003-
null,
1004-
0,
1005-
0,
1006-
0,
1007-
0 /* coordinates */,
1008-
false,
1009-
false,
1010-
false,
1011-
false /* modifier keys */,
1012-
0 /*left*/,
1013-
null
1014-
);
939+
var evt = createMouseEvent();
1015940

1016941
function kaboom() {
1017942
throw new Error('lol');
@@ -1049,31 +974,8 @@ describe('integration', function() {
1049974
Raven._breadcrumbs = [];
1050975

1051976
// keypress <input/> twice
1052-
var keypress1 = document.createEvent('KeyboardEvent');
1053-
keypress1.initKeyboardEvent(
1054-
'keypress',
1055-
true,
1056-
true,
1057-
window,
1058-
'b',
1059-
66,
1060-
0,
1061-
'',
1062-
false
1063-
);
1064-
1065-
var keypress2 = document.createEvent('KeyboardEvent');
1066-
keypress2.initKeyboardEvent(
1067-
'keypress',
1068-
true,
1069-
true,
1070-
window,
1071-
'a',
1072-
65,
1073-
0,
1074-
'',
1075-
false
1076-
);
977+
var keypress1 = createKeyboardEvent('a');
978+
var keypress2 = createKeyboardEvent('b');
1077979

1078980
var input = document.getElementsByTagName('input')[0];
1079981
input.dispatchEvent(keypress1);
@@ -1107,18 +1009,7 @@ describe('integration', function() {
11071009
Raven._breadcrumbs = [];
11081010

11091011
// keypress <input/>
1110-
var keypress = document.createEvent('KeyboardEvent');
1111-
keypress.initKeyboardEvent(
1112-
'keypress',
1113-
true,
1114-
true,
1115-
window,
1116-
'b',
1117-
66,
1118-
0,
1119-
'',
1120-
false
1121-
);
1012+
var keypress = createKeyboardEvent();
11221013

11231014
var input = document.getElementsByTagName('input')[0];
11241015
input.dispatchEvent(keypress);
@@ -1156,52 +1047,11 @@ describe('integration', function() {
11561047
Raven._breadcrumbs = [];
11571048

11581049
// 1st keypress <input/>
1159-
var keypress1 = document.createEvent('KeyboardEvent');
1160-
keypress1.initKeyboardEvent(
1161-
'keypress',
1162-
true,
1163-
true,
1164-
window,
1165-
'b',
1166-
66,
1167-
0,
1168-
'',
1169-
false
1170-
);
1171-
1050+
var keypress1 = createKeyboardEvent('a');
11721051
// click <input/>
1173-
var click = document.createEvent('MouseEvent');
1174-
click.initMouseEvent(
1175-
'click',
1176-
true /* bubble */,
1177-
true /* cancelable */,
1178-
window,
1179-
null,
1180-
0,
1181-
0,
1182-
0,
1183-
0 /* coordinates */,
1184-
false,
1185-
false,
1186-
false,
1187-
false /* modifier keys */,
1188-
0 /*left*/,
1189-
null
1190-
);
1191-
1052+
var click = createMouseEvent();
11921053
// 2nd keypress
1193-
var keypress2 = document.createEvent('KeyboardEvent');
1194-
keypress2.initKeyboardEvent(
1195-
'keypress',
1196-
true,
1197-
true,
1198-
window,
1199-
'a',
1200-
65,
1201-
0,
1202-
'',
1203-
false
1204-
);
1054+
var keypress2 = createKeyboardEvent('b');
12051055

12061056
var input = document.getElementsByTagName('input')[0];
12071057
input.dispatchEvent(keypress1);
@@ -1251,31 +1101,8 @@ describe('integration', function() {
12511101
Raven._breadcrumbs = [];
12521102

12531103
// keypress <input/> twice
1254-
var keypress1 = document.createEvent('KeyboardEvent');
1255-
keypress1.initKeyboardEvent(
1256-
'keypress',
1257-
true,
1258-
true,
1259-
window,
1260-
'b',
1261-
66,
1262-
0,
1263-
'',
1264-
false
1265-
);
1266-
1267-
var keypress2 = document.createEvent('KeyboardEvent');
1268-
keypress2.initKeyboardEvent(
1269-
'keypress',
1270-
true,
1271-
true,
1272-
window,
1273-
'a',
1274-
65,
1275-
0,
1276-
'',
1277-
false
1278-
);
1104+
var keypress1 = createKeyboardEvent('a');
1105+
var keypress2 = createKeyboardEvent('b');
12791106

12801107
var div = document.querySelector('[contenteditable]');
12811108
div.dispatchEvent(keypress1);

0 commit comments

Comments
 (0)