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

Commit

Permalink
feat(reporter): expose mocha test properties
Browse files Browse the repository at this point in the history
  • Loading branch information
danthareja committed Sep 24, 2016
1 parent 7b41f52 commit e4eb5fd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
22 changes: 19 additions & 3 deletions README.md
Expand Up @@ -84,8 +84,8 @@ module.exports = function(config) {
config.set({
...
client: {
mocha:{
grep: '<pattern>',
mocha: {
grep: '<pattern>', // passed directly to mocha
...
}
...
Expand All @@ -94,7 +94,22 @@ module.exports = function(config) {
};
```

The `grep` argument is passed directly to mocha.
If you want to expose test properties specific to `mocha`, you can use the `expose` option:

```js
module.exports = function(config) {
config.set({
...
client: {
mocha: {
expose: ['body'] // This will be exposed in a reporter as `result.mocha.body`
...
}
...
}
});
};
```

## Internals

Expand All @@ -114,6 +129,7 @@ On the end of each test `karma-mocha` passes to `karma` result object with field
* `actual` Actual data in assertion, serialized to string.
* `expected` Expected data in assertion, serialized to string.
* `showDiff` True if it is configured by assertion to show diff.
* `mocha` An optional object listed if you use the `expose` option

This object will be passed to test reporter.

Expand Down
13 changes: 11 additions & 2 deletions src/adapter.js
Expand Up @@ -151,6 +151,15 @@ var createMochaReporterConstructor = function (tc, pathname) {
pointer = pointer.parent
}

if (haveMochaConfig(tc) && tc.config.mocha.expose && tc.config.mocha.expose.forEach) {
result.mocha = {}
tc.config.mocha.expose.forEach(function (prop) {
if (test.hasOwnProperty(prop)) {
result.mocha[prop] = test[prop]
}
})
}

tc.result(result)
})
}
Expand Down Expand Up @@ -212,8 +221,8 @@ var createConfigObject = function (karma) {

// Copy all properties to mochaConfig
for (var key in karma.config.mocha) {
// except for reporter or require
if (includes(['reporter', 'require'], key)) {
// except for reporter, require, or expose
if (includes(['reporter', 'require', 'expose'], key)) {
continue
}

Expand Down
49 changes: 49 additions & 0 deletions test/adapter.spec.js
Expand Up @@ -225,6 +225,47 @@ describe('adapter mocha', function () {

expect(tc.result.called).to.eq(true)
})

it('should report mocha properties through the `expose` option', function () {
tc.config = {
mocha: {
expose: ['body', 'hello']
}
}

sandbox.stub(tc, 'result', function (result) {
expect(result.mocha.body).to.eq('function(){ expect(false).to.be(true) }')
expect(result.mocha.hello).to.eq('world')
})

var mockMochaResult = {
parent: {title: 'desc2', root: true},
title: 'should do something',
body: 'function(){ expect(false).to.be(true) }',
hello: 'world'
}

runner.emit('test end', mockMochaResult)

expect(tc.result.called).to.eq(true)
})

it('should not report mocha properties if `expose` is not configured', function () {
sandbox.stub(tc, 'result', function (result) {
expect(result.mocha).to.not.exist
})

var mockMochaResult = {
parent: {title: 'desc2', root: true},
title: 'should do something',
body: 'function(){ expect(false).to.be(true) }',
hello: 'world'
}

runner.emit('test end', mockMochaResult)

expect(tc.result.called).to.eq(true)
})
})

describe('fail', function () {
Expand Down Expand Up @@ -441,6 +482,14 @@ describe('adapter mocha', function () {
expect(createConfigObject(this.karma).require).not.to.eq('test')
})

it('should ignore property expose from client config', function () {
this.karma.config.mocha = {
expose: 'body'
}

expect(createConfigObject(this.karma).expose).not.to.eq('body')
})

it('should merge the globals from client config if they exist', function () {
this.karma.config.mocha = {
globals: ['test']
Expand Down

0 comments on commit e4eb5fd

Please sign in to comment.