Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
feat: Allow define html reporter on debug page. resolve #20
Browse files Browse the repository at this point in the history
  • Loading branch information
maksimr committed Jun 15, 2014
1 parent 6b57401 commit 7f8d4cc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ var createPattern = function(path) {
return {pattern: path, included: true, served: true, watched: false};
};

var initMocha = function(files) {
var initMocha = function(files, mochaConfig) {
var mochaPath = path.dirname(require.resolve('mocha'));
files.unshift(createPattern(__dirname + '/adapter.js'));
files.unshift(createPattern(mochaPath + '/mocha.js'));

if (mochaConfig && mochaConfig.reporter) {
files.unshift(createPattern(mochaPath + '/mocha.css'));
}
};

initMocha.$inject = ['config.files'];
initMocha.$inject = ['config.files', 'config.client.mocha'];

module.exports = {
'framework:mocha': ['factory', initMocha]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"shared-karma-files": "git://github.com/karma-runner/shared-karma-files.git#82ae8d02"
},
"peerDependencies": {
"karma": ">=0.9",
"karma": ">=0.12.8",
"mocha": "*"
},
"license": "MIT",
Expand Down
19 changes: 17 additions & 2 deletions src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,22 @@ var formatError = function(error) {
};


var createMochaReporterConstructor = function(tc) {
var createMochaReporterNode = function() {
var mochaRunnerNode = document.createElement('div');
mochaRunnerNode.setAttribute('id', 'mocha');
document.body.appendChild(mochaRunnerNode);
};

var haveMochaConfig = function(karma) {
return karma.config && karma.config.mocha;
};

var createMochaReporterConstructor = function(tc, pathname) {
// Set custom reporter on debug page
if (/debug.html$/.test(pathname) && haveMochaConfig(tc) && tc.config.mocha.reporter) {
createMochaReporterNode();
return tc.config.mocha.reporter;
}

// TODO(vojta): error formatting
return function(runner) {
Expand Down Expand Up @@ -107,7 +122,7 @@ var createMochaStartFn = function(mocha) {

// Default configuration
var mochaConfig = {
reporter: createMochaReporterConstructor(window.__karma__),
reporter: createMochaReporterConstructor(window.__karma__, window.location.pathname),
ui: 'bdd',
globals: ['__cov*']
};
Expand Down
29 changes: 29 additions & 0 deletions test/adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ describe('adapter mocha', function() {
sandbox.restore();
});

describe('createMochaReporterConstructor', function(){
beforeEach(function() {
this.karma = new Karma(new MockSocket(), null, null, null, {search: ''});
this.karma.config = {
mocha: {
reporter: 'html'
}
};

sandbox.stub(window, 'createMochaReporterNode');
});

it('should take reporter from client config on debug page', function(){
expect(createMochaReporterConstructor(this.karma, '/debug.html')).to.equal('html');
});

it('should create node for mocha reporter', function(){
createMochaReporterConstructor(this.karma, '/debug.html');

expect(createMochaReporterNode.called).to.equal(true);
});

it('should define console reporter if does not pass reporter in config', function(){
this.karma.config.mocha.reporter = null;

expect(createMochaReporterConstructor(this.karma, '/debug.html')).not.to.equal(null);
});
});

describe('reporter', function() {
var runner, tc;

Expand Down

0 comments on commit 7f8d4cc

Please sign in to comment.