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]
This commit is contained in:
Gregg Van Hove
2018-04-23 17:25:11 -07:00
parent c440d13754
commit 7e14a97371
3 changed files with 30 additions and 21 deletions

View File

@@ -487,7 +487,7 @@ getJasmineRequireObj().util = function(j$) {
return false; return false;
} }
function errorWithStack() { util.errorWithStack = function errorWithStack () {
// Don't throw and catch if we don't have to, because it makes it harder // Don't throw and catch if we don't have to, because it makes it harder
// for users to debug their code with exception breakpoints. // for users to debug their code with exception breakpoints.
var error = new Error(); var error = new Error();
@@ -502,10 +502,10 @@ getJasmineRequireObj().util = function(j$) {
} catch (e) { } catch (e) {
return e; return e;
} }
} };
function callerFile() { function callerFile() {
var trace = new j$.StackTrace(errorWithStack()); var trace = new j$.StackTrace(util.errorWithStack());
return trace.frames[2].file; return trace.frames[2].file;
} }

View File

@@ -1005,6 +1005,25 @@ describe("Env integration", function() {
describe("with a mock clock", function() { describe("with a mock clock", function() {
var realSetTimeout; var realSetTimeout;
function createMockedEnv() {
// explicitly pass in timing functions so we can make sure that clear stack always works
// no matter how long the suite in the spec is
return new jasmineUnderTest.Env({ global: {
setTimeout: function(cb, t) {
var stack = jasmine.util.errorWithStack().stack;
if (stack.indexOf('ClearStack') >= 0) {
realSetTimeout(cb, t);
} else {
setTimeout(cb, t);
}
},
clearTimeout: clearTimeout,
setInterval: setInterval,
clearInterval: clearInterval,
setImmediate: function(cb) { realSetTimeout(cb, 0); }
}});
}
beforeEach(function() { beforeEach(function() {
this.originalTimeout = jasmineUnderTest.DEFAULT_TIMEOUT_INTERVAL; this.originalTimeout = jasmineUnderTest.DEFAULT_TIMEOUT_INTERVAL;
realSetTimeout = setTimeout; realSetTimeout = setTimeout;
@@ -1019,7 +1038,7 @@ describe("Env integration", function() {
}); });
it("should wait a default interval before failing specs that haven't called done yet", function(done) { it("should wait a default interval before failing specs that haven't called done yet", function(done) {
var env = new jasmineUnderTest.Env(), var env = createMockedEnv(),
reporter = jasmine.createSpyObj('fakeReporter', [ "specDone", "jasmineDone" ]); reporter = jasmine.createSpyObj('fakeReporter', [ "specDone", "jasmineDone" ]);
reporter.specDone.and.callFake(function(result) { reporter.specDone.and.callFake(function(result) {
@@ -1048,7 +1067,7 @@ describe("Env integration", function() {
}); });
it("should not use the mock clock for asynchronous timeouts", function(done){ it("should not use the mock clock for asynchronous timeouts", function(done){
var env = new jasmineUnderTest.Env(), var env = createMockedEnv(),
reporter = jasmine.createSpyObj('fakeReporter', [ "specDone", "jasmineDone" ]), reporter = jasmine.createSpyObj('fakeReporter', [ "specDone", "jasmineDone" ]),
clock = env.clock; clock = env.clock;
@@ -1086,23 +1105,13 @@ describe("Env integration", function() {
}); });
it('should wait a custom interval before reporting async functions that fail to call done', function(done) { it('should wait a custom interval before reporting async functions that fail to call done', function(done) {
var env = new jasmineUnderTest.Env(), var env = createMockedEnv(),
reporter = jasmine.createSpyObj('fakeReport', ['jasmineDone', 'suiteDone', 'specDone']), reporter = jasmine.createSpyObj('fakeReport', ['jasmineDone', 'suiteDone', 'specDone']),
timeoutFailure = (/^Error: Timeout - Async callback was not invoked within timeout specified by jasmine\.DEFAULT_TIMEOUT_INTERVAL\./); timeoutFailure = (/^Error: Timeout - Async callback was not invoked within timeout specified by jasmine\.DEFAULT_TIMEOUT_INTERVAL\./);
reporter.specDone.and.callFake(function(r) {
realSetTimeout(function() {
jasmine.clock().tick(1);
}, 0);
});
reporter.suiteDone.and.callFake(function(r) { reporter.jasmineDone.and.callFake(function(r) {
realSetTimeout(function() { expect(r.failedExpectations).toEqual([]);
jasmine.clock().tick(1);
}, 0);
});
reporter.jasmineDone.and.callFake(function() {
expect(reporter.suiteDone).toHaveFailedExpectationsForRunnable('suite beforeAll', [ timeoutFailure ]); expect(reporter.suiteDone).toHaveFailedExpectationsForRunnable('suite beforeAll', [ timeoutFailure ]);
expect(reporter.suiteDone).toHaveFailedExpectationsForRunnable('suite afterAll', [ timeoutFailure ]); expect(reporter.suiteDone).toHaveFailedExpectationsForRunnable('suite afterAll', [ timeoutFailure ]);
expect(reporter.specDone).toHaveFailedExpectationsForRunnable('suite beforeEach times out', [ timeoutFailure ]); expect(reporter.specDone).toHaveFailedExpectationsForRunnable('suite beforeEach times out', [ timeoutFailure ]);

View File

@@ -112,7 +112,7 @@ getJasmineRequireObj().util = function(j$) {
return false; return false;
} }
function errorWithStack() { util.errorWithStack = function errorWithStack () {
// Don't throw and catch if we don't have to, because it makes it harder // Don't throw and catch if we don't have to, because it makes it harder
// for users to debug their code with exception breakpoints. // for users to debug their code with exception breakpoints.
var error = new Error(); var error = new Error();
@@ -127,10 +127,10 @@ getJasmineRequireObj().util = function(j$) {
} catch (e) { } catch (e) {
return e; return e;
} }
} };
function callerFile() { function callerFile() {
var trace = new j$.StackTrace(errorWithStack()); var trace = new j$.StackTrace(util.errorWithStack());
return trace.frames[2].file; return trace.frames[2].file;
} }