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 realSetTimeout = global.setTimeout;
var realSetTimeout = j$.getGlobal().setTimeout;
this.clock = new j$.Clock(global, new j$.DelayedFunctionScheduler());
this.spies_ = [];
@@ -416,7 +416,7 @@ getJasmineRequireObj().Env = function(j$) {
function clearStack(fn) {
currentSpecCallbackDepth++;
if (currentSpecCallbackDepth > maximumSpecCallbackDepth) {
if (currentSpecCallbackDepth >= maximumSpecCallbackDepth) {
currentSpecCallbackDepth = 0;
realSetTimeout(fn, 0);
} else {
@@ -1461,7 +1461,7 @@ getJasmineRequireObj().Suite = function() {
this.parentSuite = attrs.parentSuite;
this.description = attrs.description;
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.clearStack = attrs.clearStack || function(fn) {fn();};

View File

@@ -54,11 +54,22 @@ describe("Env", function() {
});
// 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(),
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.it("with a spec", function() {
@@ -70,16 +81,23 @@ describe("Env (integration)", function() {
});
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(),
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.it("an outer spec", function() {
@@ -96,18 +114,25 @@ describe("Env (integration)", function() {
});
env.execute();
expect(calls).toEqual([
'an outer spec',
'an inner spec',
'another inner spec'
]);
});
it("Multiple top-level Suites execute as expected", function() {
it("Multiple top-level Suites execute as expected", function(done) {
var env = new j$.Env(),
calls = [];
calls = [];
var assertions = function() {
expect(calls).toEqual([
'an outer spec',
'an inner spec',
'another inner spec',
'a 2nd outer spec'
]);
done();
};
env.addReporter({ jasmineDone: assertions });
env.describe("Outer suite", function() {
env.it("an outer spec", function() {
@@ -130,20 +155,22 @@ describe("Env (integration)", function() {
});
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'),
delayedFunctionForGlobalClock = jasmine.createSpy('delayedFunctionForGlobalClock'),
delayedFunctionForMockClock = jasmine.createSpy('delayedFunctionForMockClock'),
env = new j$.Env({global: { setTimeout: globalSetTimeout }});
delayedFunctionForGlobalClock = jasmine.createSpy('delayedFunctionForGlobalClock'),
delayedFunctionForMockClock = jasmine.createSpy('delayedFunctionForMockClock'),
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.it("test with mock clock", function() {
@@ -160,23 +187,33 @@ describe("Env (integration)", function() {
expect(delayedFunctionForMockClock).not.toHaveBeenCalled();
env.execute();
expect(delayedFunctionForMockClock).toHaveBeenCalled();
expect(globalSetTimeout).toHaveBeenCalledWith(delayedFunctionForGlobalClock, 100);
});
// 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'),
env = new j$.Env({now: fakeNow}),
reporter = jasmine.createSpyObj('fakeReporter', [
"jasmineStarted",
"jasmineDone",
"suiteStarted",
"suiteDone",
"specStarted",
"specDone"
]);
env = new j$.Env({now: fakeNow}),
reporter = jasmine.createSpyObj('fakeReporter', [
"jasmineStarted",
"jasmineDone",
"suiteStarted",
"suiteDone",
"specStarted",
"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);
reporter.suiteDone.andCallFake(function() { fakeNow.andReturn(1500); });
@@ -198,20 +235,11 @@ describe("Env (integration)", function() {
});
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() {
var env = new j$.Env({global: { setTimeout: setTimeout }}),
topLevelSpec, nestedSpec, doublyNestedSpec;
topLevelSpec, nestedSpec, doublyNestedSpec;
env.describe("my tests", function() {
topLevelSpec = env.it("are sometimes top level", function() {
@@ -231,16 +259,26 @@ describe("Env (integration)", function() {
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 }}),
reporter = jasmine.createSpyObj('fakeReproter', [
"jasmineStarted",
"jasmineDone",
"suiteStarted",
"suiteDone",
"specStarted",
"specDone"
]);
reporter = jasmine.createSpyObj('fakeReproter', [
"jasmineStarted",
"jasmineDone",
"suiteStarted",
"suiteDone",
"specStarted",
"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);
@@ -256,27 +294,21 @@ describe("Env (integration)", function() {
});
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() {
var env = new j$.Env({global: { setTimeout: setTimeout }}),
matchers = {
toFoo: function() {}
},
reporter = jasmine.createSpyObj('fakeReproter', [
"jasmineStarted",
"jasmineDone",
"suiteStarted",
"suiteDone",
"specStarted",
"specDone"
]);
matchers = {
toFoo: function() {}
},
reporter = jasmine.createSpyObj('fakeReproter', [
"jasmineStarted",
"jasmineDone",
"suiteStarted",
"suiteDone",
"specStarted",
"specDone"
]);
env.addReporter(reporter);
@@ -294,16 +326,26 @@ describe("Env (integration)", function() {
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 }}),
reporter = jasmine.createSpyObj('fakeReproter', [
"jasmineStarted",
"jasmineDone",
"suiteStarted",
"suiteDone",
"specStarted",
"specDone"
]);
reporter = jasmine.createSpyObj('fakeReproter', [
"jasmineStarted",
"jasmineDone",
"suiteStarted",
"suiteDone",
"specStarted",
"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);
@@ -319,11 +361,5 @@ describe("Env (integration)", function() {
});
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;
beforeEach(function() {

View File

@@ -32,7 +32,7 @@ describe("jasmine spec running", function () {
expect(it4.id).toEqual(4);
});
it('nested suites', function () {
it('nested suites', function (done) {
var foo = 0;
var bar = 0;
@@ -63,15 +63,16 @@ describe("jasmine spec running", function () {
expect(bar).toEqual(0);
expect(baz).toEqual(0);
expect(quux).toEqual(0);
nested.execute();
expect(foo).toEqual(1);
expect(bar).toEqual(1);
expect(baz).toEqual(1);
expect(quux).toEqual(1);
nested.execute(function() {
expect(foo).toEqual(1);
expect(bar).toEqual(1);
expect(baz).toEqual(1);
expect(quux).toEqual(1);
done();
});
});
it("should permit nested describes", function() {
it("should permit nested describes", function(done) {
var actions = [];
env.beforeEach(function () {
@@ -128,42 +129,46 @@ describe("jasmine spec running", function () {
});
});
var assertions = function() {
var expected = [
"topSuite beforeEach",
"outer beforeEach",
"outer it 1",
"outer afterEach",
"topSuite afterEach",
"topSuite beforeEach",
"outer beforeEach",
"inner 1 beforeEach",
"inner 1 it",
"inner 1 afterEach",
"outer afterEach",
"topSuite afterEach",
"topSuite beforeEach",
"outer beforeEach",
"outer it 2",
"outer afterEach",
"topSuite afterEach",
"topSuite beforeEach",
"outer beforeEach",
"inner 2 beforeEach",
"inner 2 it",
"inner 2 afterEach",
"outer afterEach",
"topSuite afterEach"
];
expect(actions).toEqual(expected);
done();
}
env.addReporter({jasmineDone: assertions});
env.execute();
var expected = [
"topSuite beforeEach",
"outer beforeEach",
"outer it 1",
"outer afterEach",
"topSuite afterEach",
"topSuite beforeEach",
"outer beforeEach",
"inner 1 beforeEach",
"inner 1 it",
"inner 1 afterEach",
"outer afterEach",
"topSuite afterEach",
"topSuite beforeEach",
"outer beforeEach",
"outer it 2",
"outer afterEach",
"topSuite afterEach",
"topSuite beforeEach",
"outer beforeEach",
"inner 2 beforeEach",
"inner 2 it",
"inner 2 afterEach",
"outer afterEach",
"topSuite afterEach"
];
expect(actions).toEqual(expected);
});
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 = [];
env.beforeEach(function () {
@@ -204,23 +209,28 @@ describe("jasmine spec running", function () {
});
});
env.execute();
var assertions = function() {
var expected = [
"runner beforeEach1",
"runner beforeEach2",
"beforeEach1",
"beforeEach2",
"outer it 1",
"afterEach2",
"afterEach1",
"runner afterEach2",
"runner afterEach1"
];
expect(actions).toEqual(expected);
done();
};
var expected = [
"runner beforeEach1",
"runner beforeEach2",
"beforeEach1",
"beforeEach2",
"outer it 1",
"afterEach2",
"afterEach1",
"runner afterEach2",
"runner afterEach1"
];
expect(actions).toEqual(expected);
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"),
suite = env.describe('A Suite', function() {
env.xdescribe('with a disabled suite', function(){
@@ -228,20 +238,22 @@ describe("jasmine spec running", function () {
});
});
suite.execute();
expect(specInADisabledSuite).not.toHaveBeenCalled();
suite.execute(function() {
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,
suite = env.describe('default current suite', function() {
pendingSpec = env.it("I am a pending spec");
});
suite.execute();
expect(pendingSpec.status()).toBe("pending");
suite.execute(function() {
expect(pendingSpec.status()).toBe("pending");
done();
});
});

View File

@@ -150,7 +150,7 @@ describe('Spies', function () {
expect(originalFunctionWasCalled).toEqual(true);
});
it('calls removeAllSpies during spec finish', function() {
it('calls removeAllSpies during spec finish', function(done) {
var env = new j$.Env(),
originalFoo = function() {},
testObj = {
@@ -167,9 +167,15 @@ describe('Spies', function () {
env.it('spec 1', secondSpec);
});
var assertions = function() {
expect(firstSpec).toHaveBeenCalled();
expect(secondSpec).toHaveBeenCalled();
done();
};
env.addReporter({ jasmineDone: assertions });
env.execute();
expect(firstSpec).toHaveBeenCalled();
expect(secondSpec).toHaveBeenCalled();
});
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 realSetTimeout = global.setTimeout;
var realSetTimeout = j$.getGlobal().setTimeout;
this.clock = new j$.Clock(global, new j$.DelayedFunctionScheduler());
this.spies_ = [];
@@ -111,7 +111,7 @@ getJasmineRequireObj().Env = function(j$) {
function clearStack(fn) {
currentSpecCallbackDepth++;
if (currentSpecCallbackDepth > maximumSpecCallbackDepth) {
if (currentSpecCallbackDepth >= maximumSpecCallbackDepth) {
currentSpecCallbackDepth = 0;
realSetTimeout(fn, 0);
} else {

View File

@@ -5,7 +5,7 @@ getJasmineRequireObj().Suite = function() {
this.parentSuite = attrs.parentSuite;
this.description = attrs.description;
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.clearStack = attrs.clearStack || function(fn) {fn();};