Skip to content

Commit ec0030e

Browse files
committed
Add basic integration tests
1 parent 3844fb6 commit ec0030e

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed

test/integration/frame.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>JS Bin</title>
6+
<script src="../../dist/raven.js"></script>
7+
<script>
8+
Raven._makeRequest = function () {};
9+
10+
Raven.config('https://public@example.com/1', {
11+
dataCallback: function (data) {
12+
window.ravenData = data;
13+
}
14+
}).install();
15+
16+
function foo() {
17+
derp();
18+
}
19+
</script>
20+
</head>
21+
<body>

test/integration/index.html

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Raven.js Test Suite</title>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6+
<link rel="stylesheet" href="../../node_modules/grunt-mocha/node_modules/mocha/mocha.css" />
7+
</head>
8+
<body>
9+
<div id="mocha"></div>
10+
<!-- Mocha -->
11+
<script src="../../node_modules/grunt-mocha/node_modules/mocha/mocha.js"></script>
12+
<script src="../../node_modules/chai/chai.js"></script>
13+
<script>
14+
mocha.ui('bdd');
15+
mocha.reporter('html');
16+
var assert = chai.assert
17+
</script>
18+
19+
<!-- Mocking -->
20+
<script src="../../node_modules/sinon/pkg/sinon.js"></script>
21+
<script src="../../dist/raven.js"></script>
22+
<script>
23+
beforeEach(function() {
24+
this.sinon = sinon.sandbox.create();
25+
var xhr = sinon.useFakeXMLHttpRequest();
26+
window.requests = this.requests = [];
27+
28+
xhr.onCreate = function (xhr) {
29+
requests.push(xhr);
30+
};
31+
});
32+
33+
afterEach(function() {
34+
this.sinon.restore();
35+
});
36+
</script>
37+
38+
<!-- Tests -->
39+
<script src="test.js"></script>
40+
41+
<script>
42+
(function(runner){
43+
window.runner = runner;
44+
var failed = [];
45+
46+
runner.on('fail', function(test, err){
47+
failed.push({
48+
title: test.title,
49+
fullTitle: test.fullTitle(),
50+
error: {
51+
message: err.message,
52+
stack: err.stack
53+
}
54+
});
55+
});
56+
57+
runner.on('end', function(){
58+
runner.stats.failed = failed;
59+
window.mochaResults = runner.stats;
60+
});
61+
})(mocha.run());
62+
</script>
63+
</body>
64+
</html>

test/integration/test.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*global assert*/
2+
function iframeExecute(iframe, done, execute, assertCallback) {
3+
iframe.contentWindow.done = function () {
4+
try {
5+
assertCallback(iframe);
6+
done();
7+
} catch (e) {
8+
done(e);
9+
}
10+
}
11+
iframe.contentWindow.eval('(' + execute.toString() + ')();');
12+
}
13+
14+
describe('integration', function () {
15+
beforeEach(function (done) {
16+
var iframe = this.iframe = document.createElement('iframe');
17+
iframe.style.display = 'none';
18+
iframe.src = './frame.html';
19+
iframe.onload = function () {
20+
done();
21+
};
22+
document.body.appendChild(iframe);
23+
});
24+
25+
afterEach(function () {
26+
document.body.removeChild(this.iframe);
27+
});
28+
29+
describe('native', function () {
30+
it('should capture exceptions from event listeners', function (done) {
31+
var iframe = this.iframe;
32+
33+
iframeExecute(iframe, done,
34+
function () {
35+
var div = document.createElement('div');
36+
document.body.appendChild(div);
37+
div.addEventListener('click', function () {
38+
foo();
39+
}, false);
40+
41+
// use setTimeout to "normalize" stack origin
42+
setTimeout(function () {
43+
var evt;
44+
if (document.createEvent) {
45+
evt = document.createEvent('MouseEvents');
46+
evt.initEvent('click', true, false);
47+
div.dispatchEvent(evt);
48+
} else if(document.createEventObject) {
49+
div.fireEvent('onclick');
50+
}
51+
});
52+
53+
setTimeout(done);
54+
},
55+
function () {
56+
var ravenData = iframe.contentWindow.ravenData;
57+
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, 3);
58+
}
59+
);
60+
});
61+
62+
it('should capture exceptions inside setTimeout', function (done) {
63+
var iframe = this.iframe;
64+
65+
iframeExecute(iframe, done,
66+
function () {
67+
setTimeout(function () {
68+
setTimeout(done);
69+
foo();
70+
}, 10);
71+
},
72+
function () {
73+
var ravenData = iframe.contentWindow.ravenData;
74+
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, 2);
75+
}
76+
);
77+
});
78+
79+
it('should capture exceptions inside setInterval', function (done) {
80+
var iframe = this.iframe;
81+
82+
iframeExecute(iframe, done,
83+
function () {
84+
var exceptionInterval = setInterval(function () {
85+
setTimeout(done);
86+
clearInterval(exceptionInterval);
87+
foo();
88+
}, 10);
89+
},
90+
function () {
91+
var ravenData = iframe.contentWindow.ravenData;
92+
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, 2);
93+
}
94+
);
95+
});
96+
97+
it('should capture exceptions inside requestAnimationFrame', function (done) {
98+
var iframe = this.iframe;
99+
// needs to be visible or requestAnimationFrame won't ever fire
100+
iframe.style.display = 'block';
101+
102+
iframeExecute(iframe, done,
103+
function () {
104+
requestAnimationFrame(function () {
105+
setTimeout(done);
106+
foo();
107+
});
108+
},
109+
function () {
110+
var ravenData = iframe.contentWindow.ravenData;
111+
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, 2);
112+
}
113+
);
114+
});
115+
});
116+
});

0 commit comments

Comments
 (0)