Skip to content

Commit 933647b

Browse files
committed
Determine baseline stack trace depth for assertions
1 parent 44c2e89 commit 933647b

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

src/raven.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Raven.prototype = {
7272

7373
debug: true,
7474

75+
TraceKit: TraceKit, // alias to TraceKit
76+
7577
/*
7678
* Configure Raven with a DSN and extra options
7779
*

test/integration/test.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,45 @@ function iframeExecute(iframe, done, execute, assertCallback) {
88
done(e);
99
}
1010
}
11-
iframe.contentWindow.eval('(' + execute.toString() + ')();');
11+
// use setTimeout so stack trace doesn't go all the way back to mocha test runner
12+
iframe.contentWindow.eval('setTimeout(' + execute.toString() + ');');
13+
}
14+
15+
function createIframe(done) {
16+
var 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+
return iframe;
1224
}
1325

1426
describe('integration', function () {
27+
var defaultStackSize = 0;
28+
29+
before(function (done) {
30+
// Before running any tests, throw/catch a known error
31+
// inside setTimeout to get a baseline expected stack
32+
// depth for future errors (this is different in every
33+
// browser).
34+
var iframe = createIframe(function () {
35+
iframe.contentWindow.setTimeout(function () {
36+
try {
37+
iframe.contentWindow.foo();
38+
} catch (e) {
39+
var trace = Raven.TraceKit.computeStackTrace(e);
40+
defaultStackSize = trace.stack.length;
41+
document.body.removeChild(iframe);
42+
done();
43+
}
44+
});
45+
});
46+
});
47+
1548
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);
49+
var iframe = this.iframe = createIframe(done);
2350
});
2451

2552
afterEach(function () {
@@ -87,7 +114,7 @@ describe('integration', function () {
87114
},
88115
function () {
89116
var ravenData = iframe.contentWindow.ravenData;
90-
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, 4);
117+
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, defaultStackSize + 1);
91118
}
92119
);
93120
});
@@ -104,7 +131,7 @@ describe('integration', function () {
104131
},
105132
function () {
106133
var ravenData = iframe.contentWindow.ravenData;
107-
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, 3);
134+
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, defaultStackSize);
108135
}
109136
);
110137
});
@@ -122,7 +149,7 @@ describe('integration', function () {
122149
},
123150
function () {
124151
var ravenData = iframe.contentWindow.ravenData;
125-
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, 3);
152+
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, defaultStackSize);
126153
}
127154
);
128155
});
@@ -141,7 +168,7 @@ describe('integration', function () {
141168
},
142169
function () {
143170
var ravenData = iframe.contentWindow.ravenData;
144-
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, 3);
171+
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, defaultStackSize);
145172
}
146173
);
147174
});

0 commit comments

Comments
 (0)