Skip to content

Commit 7e14a97

Browse files
author
Gregg Van Hove
committed
Explicitly pass in timing functions in mock clock integration specs
- This way we can make sure that clear stack always works no matter how long the suite in the spec is [fixes #153518103]
1 parent c440d13 commit 7e14a97

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

lib/jasmine-core/jasmine.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ getJasmineRequireObj().util = function(j$) {
487487
return false;
488488
}
489489

490-
function errorWithStack() {
490+
util.errorWithStack = function errorWithStack () {
491491
// Don't throw and catch if we don't have to, because it makes it harder
492492
// for users to debug their code with exception breakpoints.
493493
var error = new Error();
@@ -502,10 +502,10 @@ getJasmineRequireObj().util = function(j$) {
502502
} catch (e) {
503503
return e;
504504
}
505-
}
505+
};
506506

507507
function callerFile() {
508-
var trace = new j$.StackTrace(errorWithStack());
508+
var trace = new j$.StackTrace(util.errorWithStack());
509509
return trace.frames[2].file;
510510
}
511511

spec/core/integration/EnvSpec.js

+24-15
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,25 @@ describe("Env integration", function() {
10051005

10061006
describe("with a mock clock", function() {
10071007
var realSetTimeout;
1008+
function createMockedEnv() {
1009+
// explicitly pass in timing functions so we can make sure that clear stack always works
1010+
// no matter how long the suite in the spec is
1011+
return new jasmineUnderTest.Env({ global: {
1012+
setTimeout: function(cb, t) {
1013+
var stack = jasmine.util.errorWithStack().stack;
1014+
if (stack.indexOf('ClearStack') >= 0) {
1015+
realSetTimeout(cb, t);
1016+
} else {
1017+
setTimeout(cb, t);
1018+
}
1019+
},
1020+
clearTimeout: clearTimeout,
1021+
setInterval: setInterval,
1022+
clearInterval: clearInterval,
1023+
setImmediate: function(cb) { realSetTimeout(cb, 0); }
1024+
}});
1025+
}
1026+
10081027
beforeEach(function() {
10091028
this.originalTimeout = jasmineUnderTest.DEFAULT_TIMEOUT_INTERVAL;
10101029
realSetTimeout = setTimeout;
@@ -1019,7 +1038,7 @@ describe("Env integration", function() {
10191038
});
10201039

10211040
it("should wait a default interval before failing specs that haven't called done yet", function(done) {
1022-
var env = new jasmineUnderTest.Env(),
1041+
var env = createMockedEnv(),
10231042
reporter = jasmine.createSpyObj('fakeReporter', [ "specDone", "jasmineDone" ]);
10241043

10251044
reporter.specDone.and.callFake(function(result) {
@@ -1048,7 +1067,7 @@ describe("Env integration", function() {
10481067
});
10491068

10501069
it("should not use the mock clock for asynchronous timeouts", function(done){
1051-
var env = new jasmineUnderTest.Env(),
1070+
var env = createMockedEnv(),
10521071
reporter = jasmine.createSpyObj('fakeReporter', [ "specDone", "jasmineDone" ]),
10531072
clock = env.clock;
10541073

@@ -1086,23 +1105,13 @@ describe("Env integration", function() {
10861105
});
10871106

10881107
it('should wait a custom interval before reporting async functions that fail to call done', function(done) {
1089-
var env = new jasmineUnderTest.Env(),
1108+
var env = createMockedEnv(),
10901109
reporter = jasmine.createSpyObj('fakeReport', ['jasmineDone', 'suiteDone', 'specDone']),
10911110
timeoutFailure = (/^Error: Timeout - Async callback was not invoked within timeout specified by jasmine\.DEFAULT_TIMEOUT_INTERVAL\./);
10921111

1093-
reporter.specDone.and.callFake(function(r) {
1094-
realSetTimeout(function() {
1095-
jasmine.clock().tick(1);
1096-
}, 0);
1097-
});
1098-
1099-
reporter.suiteDone.and.callFake(function(r) {
1100-
realSetTimeout(function() {
1101-
jasmine.clock().tick(1);
1102-
}, 0);
1103-
});
11041112

1105-
reporter.jasmineDone.and.callFake(function() {
1113+
reporter.jasmineDone.and.callFake(function(r) {
1114+
expect(r.failedExpectations).toEqual([]);
11061115
expect(reporter.suiteDone).toHaveFailedExpectationsForRunnable('suite beforeAll', [ timeoutFailure ]);
11071116
expect(reporter.suiteDone).toHaveFailedExpectationsForRunnable('suite afterAll', [ timeoutFailure ]);
11081117
expect(reporter.specDone).toHaveFailedExpectationsForRunnable('suite beforeEach times out', [ timeoutFailure ]);

src/core/util.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ getJasmineRequireObj().util = function(j$) {
112112
return false;
113113
}
114114

115-
function errorWithStack() {
115+
util.errorWithStack = function errorWithStack () {
116116
// Don't throw and catch if we don't have to, because it makes it harder
117117
// for users to debug their code with exception breakpoints.
118118
var error = new Error();
@@ -127,10 +127,10 @@ getJasmineRequireObj().util = function(j$) {
127127
} catch (e) {
128128
return e;
129129
}
130-
}
130+
};
131131

132132
function callerFile() {
133-
var trace = new j$.StackTrace(errorWithStack());
133+
var trace = new j$.StackTrace(util.errorWithStack());
134134
return trace.frames[2].file;
135135
}
136136

0 commit comments

Comments
 (0)