Skip to content

Commit

Permalink
Complete rewrite of the adapter
Browse files Browse the repository at this point in the history
 - now jasmine runs the tests, and jstd is just a reporting pipeline
 - added iit and ddescribe for exclusive runs.
  • Loading branch information
mhevery committed Dec 15, 2010
1 parent 3024960 commit 477d62a
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 104 deletions.
6 changes: 6 additions & 0 deletions jsTestDriverIit.conf
@@ -0,0 +1,6 @@
server: http://localhost:9876

load:
- lib/jasmine/*.js
- src/*.js
- src-test/iit/*.js
Binary file modified lib/jstestdriver/JsTestDriver.jar
Binary file not shown.
46 changes: 46 additions & 0 deletions src-test/iit/iitSpec.js
@@ -0,0 +1,46 @@
(function() {
var specs = [];


describe('root describe', function() {
it('nested it', function() {
specs.push('nested it');
});
});


describe('root describe with iit', function() {
it('it as iit sibling', function() {
specs.push('it as iit sibling');
});

iit('nested iit', function() {
specs.push('nested iit');
});
});


describe('describe that follows iit with a nested it', function() {
it('nested it after iit', function() {
specs.push('nested it after iit');
});
});


describe('describe with iit followed by it', function() {
iit('iit that preceeds an it', function() {
specs.push('iit that preceeds an it');
});

it('it that follows an iit', function() {
specs.push('it that follows an iit');
});
});


describe('test summary', function() {
iit('should have executed all iit tests and nothing else', function() {
expect(specs).toEqual(['nested iit', 'iit that preceeds an it']);
});
});
})();
231 changes: 128 additions & 103 deletions src/JasmineAdapter.js
@@ -1,111 +1,136 @@
/**
* @fileoverview Jasmine JsTestDriver Adapter.
* @author ibolmo@gmail.com (Olmo Maldonado)
* @author misko@hevery.com (Misko Hevery)
*/

(function() {

function bind(_this, _function){
return function(){
return _function.call(_this);
};
(function(window) {
var testNameFilter;
var describePath = [];
var exclusive = {
count: 0
};
function exclusiveRemove(name) {
var name = describePath.join(' ') + ':' + (name||'');
if (exclusive[name]) {
exclusive.count--;
delete exclusive[name];
}
}

var currentFrame = frame(null, null);

function frame(parent, name){
var caseName = (parent && parent.caseName ? parent.caseName + " " : '') + (name ? name : '');
var frame = {
name: name,
caseName: caseName,
parent: parent,
testCase: TestCase(caseName),
before: [],
after: [],
runBefore: function(){
if (parent) parent.runBefore.apply(this);
for ( var i = 0; i < frame.before.length; i++) {
frame.before[i].apply(this);
}
function exclusiveAdd(name) {
var name = describePath.join(' ') + ':' + (name||'');
if (!exclusive[name]) {
exclusive.count++;
exclusive[name] = true;
}
}
window.it = function(name, fn){
exclusiveRemove(name);
jasmine.getEnv().it(name, fn);
};
window.iit = function(name, fn){
exclusiveAdd(name);
jasmine.getEnv().it(name, fn);
};
window.describe = function(name, fn){
try {
describePath.push(name);
exclusiveRemove();
jasmine.getEnv().describe(name, fn);
} finally {
describePath.pop(name);
}
};
window.ddescribe = function(name, fn){
try {
describePath.push(name);
exclusiveAdd();
jasmine.getEnv().describe(name, fn);
} finally {
describePath.pop(name);
}
};
var jasminePlugin = {
name:'jasmine',
getTestRunsConfigurationFor:function(testCaseInfos, filterExpressions, testRunsConfiguration) {
testNameFilter = function(name){
return !filterExpressions ||
filterExpressions == 'all' ||
filterExpressions == '*' ||
name.indexOf(filterExpressions) > -1;
};
var describe = new jstestdriver.TestCaseInfo('jasmine runner', function(){});
testRunsConfiguration.push(new jstestdriver.TestRunConfiguration(describe, []));
},
runTestConfiguration: function(testRunConfiguration, onTestDone, onTestRunConfigurationComplete){
var jasmineEnv = jasmine.getEnv();
var specLog = jstestdriver.console.log_ = [];
var start;
jasmineEnv.specFilter = function(spec) {
var name = spec.suite.getFullName() + ':';
var fullName = name + spec.description;
return testNameFilter(fullName) &&
(!exclusive.count || exclusive[name] || exclusive[fullName]);
};
jasmineEnv.reporter = {
log: function(str){
specLog.push(str);
},

reportRunnerStarting: function(runner) { },

reportSpecStarting: function(spec) {
specLog = jstestdriver.console.log_ = [];
start = new Date().getTime();
},

reportSpecResults: function(spec) {
var suite = spec.suite;
var results = spec.results();
if (results.skipped) return;
var end = new Date().getTime();
var messages = [];
var resultItems = results.getItems();
var state = 'passed';
for ( var i = 0; i < resultItems.length; i++) {
if (!resultItems[i].passed()) {
state = resultItems[i].message.match(/AssertionError:/) ? 'error' : 'failed';
messages.push(resultItems[i].toString());
messages.push(resultItems[i].trace.stack);
}
}
onTestDone(
new jstestdriver.TestResult(
suite.getFullName(),
spec.description,
state,
messages.join('\n'),
specLog.join('\n'),
end - start));
},

reportSuiteResults: function(suite) {},

reportRunnerResults: function(runner) {
onTestRunConfigurationComplete();
}
};
jasmineEnv.execute();
return true;
},
runAfter: function(){
for ( var i = 0; i < frame.after.length; i++) {
frame.after[i].apply(this);
}
if (parent) parent.runAfter.apply(this);
}
};
return frame;
onTestsFinish: function(){}
};
jstestdriver.pluginRegistrar.register(jasminePlugin);
})(window);

// Patch Jasmine for proper stack traces
jasmine.Spec.prototype.fail = function (e) {
var expectationResult = new jasmine.ExpectationResult({
passed: false,
message: e ? jasmine.util.formatException(e) : 'Exception'
});
// PATCH
if (e) {
expectationResult.trace = e;
}
this.results_.addResult(expectationResult);
};

jasmine.Env.prototype.describe = (function(describe){
return function(description){
currentFrame = frame(currentFrame, description);
var val = describe.apply(this, arguments);
currentFrame = currentFrame.parent;
return val;
};

})(jasmine.Env.prototype.describe);

var id = 0;

jasmine.Env.prototype.it = (function(it){
return function(desc, itFn){
var self = this;
var spec = it.apply(this, arguments);
var currentSpec = this.currentSpec;
if (!currentSpec.$id) {
currentSpec.$id = id++;
}
var frame = this.jstdFrame = currentFrame;
var name = 'test that it ' + desc;
if (this.jstdFrame.testCase.prototype[name])
throw "Spec with name '" + desc + "' already exists.";
this.jstdFrame.testCase.prototype[name] = function(){
jasmine.getEnv().currentSpec = currentSpec;
frame.runBefore.apply(currentSpec);
try {
currentSpec.queue.start();
} finally {
frame.runAfter.apply(currentSpec);
}
};
return spec;
};

})(jasmine.Env.prototype.it);


jasmine.Env.prototype.beforeEach = (function(beforeEach){
return function(beforeEachFunction) {
beforeEach.apply(this, arguments);
currentFrame.before.push(beforeEachFunction);
};

})(jasmine.Env.prototype.beforeEach);


jasmine.Env.prototype.afterEach = (function(afterEach){
return function(afterEachFunction) {
afterEach.apply(this, arguments);
currentFrame.after.push(afterEachFunction);
};

})(jasmine.Env.prototype.afterEach);


jasmine.NestedResults.prototype.addResult = (function(addResult){
return function(result) {
addResult.call(this, result);
if (result.type != 'MessageResult' && !result.passed()) fail(result.message);
};

})(jasmine.NestedResults.prototype.addResult);

// Reset environment with overriden methods.
jasmine.currentEnv_ = null;
jasmine.getEnv();

})();
1 change: 1 addition & 0 deletions test-iit.sh
@@ -0,0 +1 @@
java -jar lib/jstestdriver/JsTestDriver.jar --config jsTestDriverIit.conf --tests "*" --reset
1 change: 1 addition & 0 deletions test-regular.sh
@@ -0,0 +1 @@
java -jar lib/jstestdriver/JsTestDriver.jar --tests '' --reset
3 changes: 2 additions & 1 deletion test.sh
@@ -1 +1,2 @@
java -jar lib/jstestdriver/JsTestDriver.jar --tests all --reset
source test-regular.sh
source test-iit.sh

0 comments on commit 477d62a

Please sign in to comment.