Skip to content

Instantly share code, notes, and snippets.

@norrs
Created July 22, 2013 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norrs/6054404 to your computer and use it in GitHub Desktop.
Save norrs/6054404 to your computer and use it in GitHub Desktop.
var buster = require("buster");
var resources = require("buster-resources");
var extension = require("../lib/buster-coverage");
var sinon = require("buster-sinon");
var testRunner = buster.testRunner;
var testCase = buster.testCase;
//var testRunner = require("buster-test").testRunner;
var browserRunner = require("buster-test-cli").runners.browser;
var when = require("when");
var bane = require("bane");
var ramp = require("ramp");
function fakeConfig(tc) {
return bane.createEventEmitter({
resolve: tc.stub().returns(when.defer().promise),
runExtensionHook: tc.stub()
});
}
function fakeSession(tc) {
return bane.createEventEmitter({
onStart: tc.stub(),
onLoad: tc.stub().yields([{}]),
onEnd: tc.stub(),
onUnload: tc.stub(),
onAbort: tc.stub(),
end: tc.stub()
});
}
function fakeServerClient(tc) {
return {
connect: tc.stub().returns(when()),
createSession: tc.stub().returns(when(fakeSession(tc)))
};
}
buster.testCase("buster-coverage extension", {
setUp: function () {
this.config = buster.eventEmitter.create();
this.config.isModulePattern = true;
this.resourceSet = resources.resourceSet.create();
this.resourceSet.addResource({
path: "/src/foo.js",
content: "var foo = 42;"
});
this.resourceSet.loadPath.append("/src/foo.js");
this.resourceSet.addResource({
path: "/zar.js",
content: "zar = 2;"
});
this.resourceSet.loadPath.append("/zar.js");
},
"ensure all default properties exists and default values set if not changed on setup of extension": function () {
extension = extension.create(this.config);
assert.hasOwnProperty(extension.outputDirectory);
assert.match(extension.outputDirectory, 'coverage');
assert.hasOwnProperty(extension.format);
assert.match(extension.format, 'lcov');
assert.hasOwnProperty(extension.combinedResultsOnly);
assert.isFalse(extension.combinedResultsOnly);
},
"ensure extension accept a change in outPutDirectory": function () {
extension = extension.create(this.config);
extension.outputDirectory = 'awesome-coverage-testsresults';
extension.configure(this.config);
assert.hasOwnProperty(extension, 'outputDirectory');
assert.match(extension.outputDirectory, 'awesome-coverage-testsresults');
},
"ensure extension doesn't accept null or empty string for outputDirectory, but falls back to default": function () {
this.config.outputDirectory = null;
extension = extension.create(this.config);
extension.configure(this.config);
assert.hasOwnProperty(extension.outputDirectory);
assert.match(extension.outputDirectory, 'coverage');
refute.equals(extension.outputDirectory, this.config.outputDirectory);
},
"ensure load:sources adds hooks for addProcessor for instrumenting sources": function () {
extension.configure(this.config);
this.resourceSet.forEach(function (resource) {
assert.isFalse(resource.hasProcessors());
});
this.config.emit("load:resourcesConfig", this.resourceSet);
this.resourceSet.forEach(function (resource) {
assert.isTrue(resource.hasProcessors());
});
},
"ensure it processes source code as it should with instrument": function (done) {
extension.configure(this.config);
this.config.emit("load:resourcesConfig", this.resourceSet);
this.resourceSet.process().then(function (foo) {
});
var resource = this.resourceSet.get("/src/foo.js");
resource.content().then(done(function (content) {
assert.equals(content, "FJAS");
}));
},
"ensure processor for instrumenting sources skips coverage exclusions": function () {
this.config.coverageExclusions = ['templates'];
extension = extension.create(this.config);
this.resourceSet.addResource({
path: "/src/module/templates/bar.html",
content: "<html></html>",
encoding: "utf-8"
});
this.resourceSet.loadPath.append("/src/module/templates/bar.html");
extension.configure(this.config);
this.config.emit("load:resourcesConfig", this.resourceSet);
var resource = this.resourceSet.get("/src/module/templates/bar.html");
resource.process().then(function (processor_content) {
assert.match(processor_content, '<html></html>');
});
resource.content().then(function (content) {
assert.match(content, '<html></html>');
});
},
"ensure processor for instrumenting runs on /src/foo.js": function () {
extension.configure(this.config);
this.config.emit("load:resourcesConfig", this.resourceSet);
this.resourceSet.get("/src/foo.js")
.process().then(function (processorContent) {
assert.match(processorContent, '/src/foo.js", 1);');
});
},
"ensure passing fs mock is actually a mock": function () {
this.config.fs = this.stub();
this.config.fs.writeFileSync = function () { return this.spy; };
extension = extension.create(this.config);
extension.configure(this.config);
var foo = this.config.emit("load:resourcesConfig", this.resourceSet);
var resource = this.resourceSet.get("/src/foo.js");
/*resource.content().then(function (content) {
console.log(content);
});*/
var runner = testRunner.create();
var testFn = this.spy();
var context = testCase("Test", { test: testFn });
extension.testRun(runner, this.spy());
/*this.emitter = bane.createEventEmitter();
this.emit = function (event, data, clientId) {
return this.emitter.emit(event, {
data: data,
clientId: clientId
});
};
this.emit('coverage:report', 'fjas', this.spy());*/
var self = this;
resource.content().then(function (content) {
var clientSpy = self.spy();
var result = {data: content, client: clientSpy };
console.log(result);
runner.emit("coverage:report", result);
});
runner.emit("suite:end").then(function () {
});
/*resource.content().then(function (content) {
runner.emit("coverage:report", content);
});*/
/*run: function (config, options, done) {
var run = testRun.create(config, options || {}, this.logger, done);
run.start();
return run;
}*/
/*runner.on('suite:end', function () {
buster.emit('coverage:report', _$Coverage);
});*/
/*runner.runContext(context).then(function (done) {
resource.content().then(function (content) {
this.emit("coverage:report", content);
});
});*/
/*this.runner.runContext(context).then(test.end(function () {
assert(testFn.calledOnce);
assert(context.testCase.isPrototypeOf(testFn.thisValues[0]));
}));*/
//suite:end
//assert(this.config.fs.called);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment