Test asynchronous parts of Jasmine asynchronously

This commit is contained in:
Davis W. Frank and Sheel Choksi
2013-07-02 12:09:48 -07:00
parent f5bc9faf63
commit dcf7a0867e
7 changed files with 220 additions and 166 deletions

View File

@@ -312,7 +312,7 @@ getJasmineRequireObj().Env = function(j$) {
var catchExceptions = true; var catchExceptions = true;
var realSetTimeout = global.setTimeout; var realSetTimeout = j$.getGlobal().setTimeout;
this.clock = new j$.Clock(global, new j$.DelayedFunctionScheduler()); this.clock = new j$.Clock(global, new j$.DelayedFunctionScheduler());
this.spies_ = []; this.spies_ = [];
@@ -416,7 +416,7 @@ getJasmineRequireObj().Env = function(j$) {
function clearStack(fn) { function clearStack(fn) {
currentSpecCallbackDepth++; currentSpecCallbackDepth++;
if (currentSpecCallbackDepth > maximumSpecCallbackDepth) { if (currentSpecCallbackDepth >= maximumSpecCallbackDepth) {
currentSpecCallbackDepth = 0; currentSpecCallbackDepth = 0;
realSetTimeout(fn, 0); realSetTimeout(fn, 0);
} else { } else {
@@ -1461,7 +1461,7 @@ getJasmineRequireObj().Suite = function() {
this.parentSuite = attrs.parentSuite; this.parentSuite = attrs.parentSuite;
this.description = attrs.description; this.description = attrs.description;
this.onStart = attrs.onStart || function() {}; this.onStart = attrs.onStart || function() {};
this.completeCallback = attrs.completeCallback || function() {}; this.completeCallback = attrs.completeCallback || function() {}; // TODO: this is unused
this.resultCallback = attrs.resultCallback || function() {}; this.resultCallback = attrs.resultCallback || function() {};
this.clearStack = attrs.clearStack || function(fn) {fn();}; this.clearStack = attrs.clearStack || function(fn) {fn();};

View File

@@ -54,12 +54,23 @@ describe("Env", function() {
}); });
// TODO: move these into a separate file // TODO: move these into a separate file
describe("Env (integration)", function() { describe("Env integration", function() {
it("Suites execute as expected (no nesting)", function() { it("Suites execute as expected (no nesting)", function(done) {
var env = new j$.Env(), var env = new j$.Env(),
calls = []; calls = [];
var assertions = function() {
expect(calls).toEqual([
"with a spec",
"and another spec"
]);
done();
};
env.addReporter({ jasmineDone: assertions});
env.describe("A Suite", function() { env.describe("A Suite", function() {
env.it("with a spec", function() { env.it("with a spec", function() {
calls.push("with a spec"); calls.push("with a spec");
@@ -70,17 +81,24 @@ describe("Env (integration)", function() {
}); });
env.execute(); env.execute();
expect(calls).toEqual([
"with a spec",
"and another spec"
]);
}); });
it("Nested Suites execute as expected", function() { it("Nested Suites execute as expected", function(done) {
var env = new j$.Env(), var env = new j$.Env(),
calls = []; calls = [];
var assertions = function() {
expect(calls).toEqual([
'an outer spec',
'an inner spec',
'another inner spec'
]);
done();
};
env.addReporter({ jasmineDone: assertions });
env.describe("Outer suite", function() { env.describe("Outer suite", function() {
env.it("an outer spec", function() { env.it("an outer spec", function() {
calls.push('an outer spec') calls.push('an outer spec')
@@ -96,18 +114,25 @@ describe("Env (integration)", function() {
}); });
env.execute(); env.execute();
});
it("Multiple top-level Suites execute as expected", function(done) {
var env = new j$.Env(),
calls = [];
var assertions = function() {
expect(calls).toEqual([ expect(calls).toEqual([
'an outer spec', 'an outer spec',
'an inner spec', 'an inner spec',
'another inner spec' 'another inner spec',
'a 2nd outer spec'
]); ]);
}); done();
};
env.addReporter({ jasmineDone: assertions });
it("Multiple top-level Suites execute as expected", function() {
var env = new j$.Env(),
calls = [];
env.describe("Outer suite", function() { env.describe("Outer suite", function() {
env.it("an outer spec", function() { env.it("an outer spec", function() {
@@ -130,21 +155,23 @@ describe("Env (integration)", function() {
}); });
env.execute(); env.execute();
expect(calls).toEqual([
'an outer spec',
'an inner spec',
'another inner spec',
'a 2nd outer spec'
]);
}); });
it("Mock clock can be installed and used in tests", function() { it("Mock clock can be installed and used in tests", function(done) {
var globalSetTimeout = jasmine.createSpy('globalSetTimeout'), var globalSetTimeout = jasmine.createSpy('globalSetTimeout'),
delayedFunctionForGlobalClock = jasmine.createSpy('delayedFunctionForGlobalClock'), delayedFunctionForGlobalClock = jasmine.createSpy('delayedFunctionForGlobalClock'),
delayedFunctionForMockClock = jasmine.createSpy('delayedFunctionForMockClock'), delayedFunctionForMockClock = jasmine.createSpy('delayedFunctionForMockClock'),
env = new j$.Env({global: { setTimeout: globalSetTimeout }}); env = new j$.Env({global: { setTimeout: globalSetTimeout }});
var assertions = function() {
expect(delayedFunctionForMockClock).toHaveBeenCalled();
expect(globalSetTimeout).toHaveBeenCalledWith(delayedFunctionForGlobalClock, 100);
done();
};
env.addReporter({ jasmineDone: assertions });
env.describe("tests", function() { env.describe("tests", function() {
env.it("test with mock clock", function() { env.it("test with mock clock", function() {
env.clock.install(); env.clock.install();
@@ -160,13 +187,10 @@ describe("Env (integration)", function() {
expect(delayedFunctionForMockClock).not.toHaveBeenCalled(); expect(delayedFunctionForMockClock).not.toHaveBeenCalled();
env.execute(); env.execute();
expect(delayedFunctionForMockClock).toHaveBeenCalled();
expect(globalSetTimeout).toHaveBeenCalledWith(delayedFunctionForGlobalClock, 100);
}); });
// TODO: something is wrong with this spec // TODO: something is wrong with this spec
it("should report as expected", function() { it("should report as expected", function(done) {
var fakeNow = jasmine.createSpy('fake Date.now'), var fakeNow = jasmine.createSpy('fake Date.now'),
env = new j$.Env({now: fakeNow}), env = new j$.Env({now: fakeNow}),
reporter = jasmine.createSpyObj('fakeReporter', [ reporter = jasmine.createSpyObj('fakeReporter', [
@@ -178,6 +202,19 @@ describe("Env (integration)", function() {
"specDone" "specDone"
]); ]);
reporter.jasmineDone.andCallFake(function() {
expect(reporter.jasmineStarted).toHaveBeenCalledWith({
totalSpecsDefined: 3
});
var suiteResult = reporter.suiteStarted.calls[0].args[0];
expect(suiteResult.description).toEqual("A Suite");
expect(reporter.jasmineDone).toHaveBeenCalledWith({
executionTime: 1000
});
done();
});
fakeNow.andReturn(500); fakeNow.andReturn(500);
reporter.suiteDone.andCallFake(function() { fakeNow.andReturn(1500); }); reporter.suiteDone.andCallFake(function() { fakeNow.andReturn(1500); });
@@ -198,15 +235,6 @@ describe("Env (integration)", function() {
}); });
env.execute(); env.execute();
expect(reporter.jasmineStarted).toHaveBeenCalledWith({
totalSpecsDefined: 3
});
var suiteResult = reporter.suiteStarted.calls[0].args[0];
expect(suiteResult.description).toEqual("A Suite");
expect(reporter.jasmineDone).toHaveBeenCalledWith({
executionTime: 1000
});
}); });
it("should be possible to get full name from a spec", function() { it("should be possible to get full name from a spec", function() {
@@ -231,7 +259,7 @@ describe("Env (integration)", function() {
expect(doublyNestedSpec.getFullName()).toBe("my tests are sometimes even doubly nested."); expect(doublyNestedSpec.getFullName()).toBe("my tests are sometimes even doubly nested.");
}); });
it("Custom equality testers should be per spec", function() { it("Custom equality testers should be per spec", function(done) {
var env = new j$.Env({global: { setTimeout: setTimeout }}), var env = new j$.Env({global: { setTimeout: setTimeout }}),
reporter = jasmine.createSpyObj('fakeReproter', [ reporter = jasmine.createSpyObj('fakeReproter', [
"jasmineStarted", "jasmineStarted",
@@ -242,6 +270,16 @@ describe("Env (integration)", function() {
"specDone" "specDone"
]); ]);
reporter.jasmineDone.andCallFake(function() {
var firstSpecResult = reporter.specDone.argsForCall[0][0],
secondSpecResult = reporter.specDone.argsForCall[1][0];
expect(firstSpecResult.status).toEqual("passed");
expect(secondSpecResult.status).toEqual("failed");
done();
});
env.addReporter(reporter); env.addReporter(reporter);
env.describe("testing custom equality testers", function() { env.describe("testing custom equality testers", function() {
@@ -256,12 +294,6 @@ describe("Env (integration)", function() {
}); });
env.execute(); env.execute();
var firstSpecResult = reporter.specDone.argsForCall[0][0],
secondSpecResult = reporter.specDone.argsForCall[1][0];
expect(firstSpecResult.status).toEqual("passed");
expect(secondSpecResult.status).toEqual("failed");
}); });
it("Custom matchers should be per spec", function() { it("Custom matchers should be per spec", function() {
@@ -294,7 +326,7 @@ describe("Env (integration)", function() {
env.execute(); env.execute();
}); });
it("Custom equality testers for toContain should be per spec", function() { it("Custom equality testers for toContain should be per spec", function(done) {
var env = new j$.Env({global: { setTimeout: setTimeout }}), var env = new j$.Env({global: { setTimeout: setTimeout }}),
reporter = jasmine.createSpyObj('fakeReproter', [ reporter = jasmine.createSpyObj('fakeReproter', [
"jasmineStarted", "jasmineStarted",
@@ -305,6 +337,16 @@ describe("Env (integration)", function() {
"specDone" "specDone"
]); ]);
reporter.jasmineDone.andCallFake(function() {
var firstSpecResult = reporter.specDone.argsForCall[0][0],
secondSpecResult = reporter.specDone.argsForCall[1][0];
expect(firstSpecResult.status).toEqual("passed");
expect(secondSpecResult.status).toEqual("failed");
done();
});
env.addReporter(reporter); env.addReporter(reporter);
env.describe("testing custom equality testers", function() { env.describe("testing custom equality testers", function() {
@@ -319,11 +361,5 @@ describe("Env (integration)", function() {
}); });
env.execute(); env.execute();
var firstSpecResult = reporter.specDone.argsForCall[0][0],
secondSpecResult = reporter.specDone.argsForCall[1][0];
expect(firstSpecResult.status).toEqual("passed");
expect(secondSpecResult.status).toEqual("failed");
}); });
}); });

View File

@@ -1,4 +1,4 @@
describe('Exceptions:', function() { xdescribe('Exceptions:', function() {
var env; var env;
beforeEach(function() { beforeEach(function() {

View File

@@ -32,7 +32,7 @@ describe("jasmine spec running", function () {
expect(it4.id).toEqual(4); expect(it4.id).toEqual(4);
}); });
it('nested suites', function () { it('nested suites', function (done) {
var foo = 0; var foo = 0;
var bar = 0; var bar = 0;
@@ -63,15 +63,16 @@ describe("jasmine spec running", function () {
expect(bar).toEqual(0); expect(bar).toEqual(0);
expect(baz).toEqual(0); expect(baz).toEqual(0);
expect(quux).toEqual(0); expect(quux).toEqual(0);
nested.execute(); nested.execute(function() {
expect(foo).toEqual(1); expect(foo).toEqual(1);
expect(bar).toEqual(1); expect(bar).toEqual(1);
expect(baz).toEqual(1); expect(baz).toEqual(1);
expect(quux).toEqual(1); expect(quux).toEqual(1);
done();
});
}); });
it("should permit nested describes", function() { it("should permit nested describes", function(done) {
var actions = []; var actions = [];
env.beforeEach(function () { env.beforeEach(function () {
@@ -128,9 +129,7 @@ describe("jasmine spec running", function () {
}); });
}); });
env.execute(); var assertions = function() {
var expected = [ var expected = [
"topSuite beforeEach", "topSuite beforeEach",
"outer beforeEach", "outer beforeEach",
@@ -161,9 +160,15 @@ describe("jasmine spec running", function () {
"topSuite afterEach" "topSuite afterEach"
]; ];
expect(actions).toEqual(expected); expect(actions).toEqual(expected);
done();
}
env.addReporter({jasmineDone: assertions});
env.execute();
}); });
it("should run multiple befores and afters in the order they are declared", function() { it("should run multiple befores and afters in the order they are declared", function(done) {
var actions = []; var actions = [];
env.beforeEach(function () { env.beforeEach(function () {
@@ -204,8 +209,7 @@ describe("jasmine spec running", function () {
}); });
}); });
env.execute(); var assertions = function() {
var expected = [ var expected = [
"runner beforeEach1", "runner beforeEach1",
"runner beforeEach2", "runner beforeEach2",
@@ -218,9 +222,15 @@ describe("jasmine spec running", function () {
"runner afterEach1" "runner afterEach1"
]; ];
expect(actions).toEqual(expected); expect(actions).toEqual(expected);
done();
};
env.addReporter({jasmineDone: assertions});
env.execute();
}); });
it("shouldn't run disabled suites", function() { it("shouldn't run disabled suites", function(done) {
var specInADisabledSuite = jasmine.createSpy("specInADisabledSuite"), var specInADisabledSuite = jasmine.createSpy("specInADisabledSuite"),
suite = env.describe('A Suite', function() { suite = env.describe('A Suite', function() {
env.xdescribe('with a disabled suite', function(){ env.xdescribe('with a disabled suite', function(){
@@ -228,20 +238,22 @@ describe("jasmine spec running", function () {
}); });
}); });
suite.execute(); suite.execute(function() {
expect(specInADisabledSuite).not.toHaveBeenCalled(); expect(specInADisabledSuite).not.toHaveBeenCalled();
done();
});
}); });
it("should set all pending specs to pending when a suite is run", function() { it("should set all pending specs to pending when a suite is run", function(done) {
var pendingSpec, var pendingSpec,
suite = env.describe('default current suite', function() { suite = env.describe('default current suite', function() {
pendingSpec = env.it("I am a pending spec"); pendingSpec = env.it("I am a pending spec");
}); });
suite.execute(); suite.execute(function() {
expect(pendingSpec.status()).toBe("pending"); expect(pendingSpec.status()).toBe("pending");
done();
});
}); });

View File

@@ -150,7 +150,7 @@ describe('Spies', function () {
expect(originalFunctionWasCalled).toEqual(true); expect(originalFunctionWasCalled).toEqual(true);
}); });
it('calls removeAllSpies during spec finish', function() { it('calls removeAllSpies during spec finish', function(done) {
var env = new j$.Env(), var env = new j$.Env(),
originalFoo = function() {}, originalFoo = function() {},
testObj = { testObj = {
@@ -167,9 +167,15 @@ describe('Spies', function () {
env.it('spec 1', secondSpec); env.it('spec 1', secondSpec);
}); });
env.execute(); var assertions = function() {
expect(firstSpec).toHaveBeenCalled(); expect(firstSpec).toHaveBeenCalled();
expect(secondSpec).toHaveBeenCalled(); expect(secondSpec).toHaveBeenCalled();
done();
};
env.addReporter({ jasmineDone: assertions });
env.execute();
}); });
it('throws an exception when some method is spied on twice', function() { it('throws an exception when some method is spied on twice', function() {

View File

@@ -7,7 +7,7 @@ getJasmineRequireObj().Env = function(j$) {
var catchExceptions = true; var catchExceptions = true;
var realSetTimeout = global.setTimeout; var realSetTimeout = j$.getGlobal().setTimeout;
this.clock = new j$.Clock(global, new j$.DelayedFunctionScheduler()); this.clock = new j$.Clock(global, new j$.DelayedFunctionScheduler());
this.spies_ = []; this.spies_ = [];
@@ -111,7 +111,7 @@ getJasmineRequireObj().Env = function(j$) {
function clearStack(fn) { function clearStack(fn) {
currentSpecCallbackDepth++; currentSpecCallbackDepth++;
if (currentSpecCallbackDepth > maximumSpecCallbackDepth) { if (currentSpecCallbackDepth >= maximumSpecCallbackDepth) {
currentSpecCallbackDepth = 0; currentSpecCallbackDepth = 0;
realSetTimeout(fn, 0); realSetTimeout(fn, 0);
} else { } else {

View File

@@ -5,7 +5,7 @@ getJasmineRequireObj().Suite = function() {
this.parentSuite = attrs.parentSuite; this.parentSuite = attrs.parentSuite;
this.description = attrs.description; this.description = attrs.description;
this.onStart = attrs.onStart || function() {}; this.onStart = attrs.onStart || function() {};
this.completeCallback = attrs.completeCallback || function() {}; this.completeCallback = attrs.completeCallback || function() {}; // TODO: this is unused
this.resultCallback = attrs.resultCallback || function() {}; this.resultCallback = attrs.resultCallback || function() {};
this.clearStack = attrs.clearStack || function(fn) {fn();}; this.clearStack = attrs.clearStack || function(fn) {fn();};