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

feat: support mocha opts #121

Merged
merged 1 commit into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ module.exports = function(config) {
};
```

If you already have a configuration for Mocha in an opts file, you can use the `opts` option:

```js
module.exports = function(config) {
config.set({
...
client: {
mocha: {
opts: 'test/mocha.opts' // You can set opts to equal true then plugin will load opts from default location 'test/mocha.opts'
...
}
...
}
});
};
```

## Internals

On the end of each test `karma-mocha` passes to `karma` result object with fields:
Expand Down
62 changes: 58 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,82 @@
var path = require('path')
var extend = require('util')._extend
var minimist = require('minimist')

var createPattern = function (path) {
return {pattern: path, included: true, served: true, watched: false}
}

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

if (mochaConfig && mochaConfig.require && mochaConfig.require.map) {
config = config || {}
config.client = config.client || {}
var mochaConfig = config.client.mocha = getMochaOpts(config.client.mocha || {})

if (mochaConfig.require && mochaConfig.require.map) {
mochaConfig.require.map(function (requirePath) {
return files.unshift(createPattern(requirePath))
})
}

files.unshift(createPattern(path.join(mochaPath, 'mocha.js')))

if (mochaConfig && mochaConfig.reporter) {
if (mochaConfig.reporter) {
files.unshift(createPattern(path.join(mochaPath, 'mocha.css')))
}
}

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

module.exports = {
'framework:mocha': ['factory', initMocha]
}

function getMochaOpts (mochaConfig) {
var optsPath = typeof mochaConfig.opts === 'string' ? mochaConfig.opts : 'test/mocha.opts'

if (!mochaConfig.opts) {
return mochaConfig
}

delete mochaConfig.opts

var fs = require('fs')
if (!fs.existsSync(optsPath)) {
return mochaConfig
}

return extend(normalizeOpts(minimist(fs.readFileSync(optsPath, 'utf8')
.replace(/\\\s/g, '%20')
.split(/\s/)
.filter(Boolean)
.map(function (value) {
return value.replace(/%20/g, ' ')
}))), mochaConfig)

function normalizeOpts (opts) {
opts = [
'require',

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is there an empty line here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dignifiedquire This line separte options which will pass to mocha in browser from options which use only on the server (like require) and not acceptable in browser

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

'ui',
'reporter',
'globals',
'grep',
'timeout',
'slow',
'bail',
'ignoreLeaks'
].reduce(function (result, optName) {
if (opts.hasOwnProperty(optName)) {
result[optName] = opts[optName]
}

return result
}, {})

opts.require = [].concat(opts.require || [])

return opts
}
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"mocha"
],
"author": "Vojta Jina <vojta.jina@gmail.com>",
"dependencies": {},
"dependencies": {
"minimist": "1.2.0"
},
"devDependencies": {
"chai": "^3.4.1",
"eslint": "^2.0.0",
Expand All @@ -42,6 +44,7 @@
"karma-sinon": "^1.0.3",
"load-grunt-tasks": "^3.2.0",
"mocha": "^3.0.0",
"mock-fs": "^3.12.1",
"shared-karma-files": "git://github.com/karma-runner/shared-karma-files.git#82ae8d02",
"sinon": "^1.17.2"
},
Expand Down
89 changes: 87 additions & 2 deletions test/lib/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var mock = require('mock-fs')
var expect = require('chai').expect
var initMocha = require('../../lib')['framework:mocha'][1]

Expand All @@ -23,20 +24,104 @@ describe('framework:mocha', function () {
it('should add required files', function () {
var mochaConfig = {require: ['foo.js']}

initMocha(files, mochaConfig)
initMocha(files, createMochaConfig(mochaConfig))

expect(files[1].pattern).to.contain('foo.js')
})

it('should add mocha.css if we define reporter in config', function () {
var mochaConfig = {reporter: 'html'}

initMocha(files, mochaConfig)
initMocha(files, createMochaConfig(mochaConfig))

expect(files[0].pattern).to.contain('mocha.css')
})

describe('load mocha.opts as a configuration file', function () {
beforeEach(function () {
mock({
'test/mocha.opts': [
'--sort',

'--require should',
'--reporter dot',
'--ui bdd',
'--globals foo',
'--globals bar',
'--timeout 100',
'--slow 200',
'--grep zoo',
'--bail',
'--ignoreLeaks'
].join('\n'),
'test/foo.opts': '--ui tdd'
})
})

afterEach(function () {
mock.restore()
})

it('should read mocha options from test/mocha.opts file by default', function () {
var config = createMochaConfig({opts: true})

initMocha(files, config)

expect(config.client.mocha.require).to.eql(['should'])
expect(config.client.mocha.globals).to.eql(['foo', 'bar'])
expect(config.client.mocha.reporter).to.equal('dot')
expect(config.client.mocha.ui).to.equal('bdd')
})

it('should set only supported options', function () {
var config = createMochaConfig({opts: true})

initMocha(files, config)

expect(config.client.mocha).to.eql({
require: ['should'],

ui: 'bdd',
reporter: 'dot',
globals: ['foo', 'bar'],
grep: 'zoo',

timeout: 100,
slow: 200,
bail: true,
ignoreLeaks: true
})
})

it('should rewrite options from mocha.opts by options from config', function () {
var config = createMochaConfig({opts: true, ui: 'tdd', globals: ['zoo']})

initMocha(files, config)

expect(config.client.mocha.require).to.eql(['should'])
expect(config.client.mocha.globals).to.eql(['zoo'])
expect(config.client.mocha.reporter).to.equal('dot')
expect(config.client.mocha.ui).to.equal('tdd')
})

it('should pass custom mocha.opts path', function () {
var config = createMochaConfig({opts: 'test/foo.opts'})

initMocha(files, config)

expect(config.client.mocha.ui).to.equal('tdd')
})
})
})

function createMockFiles () {
return []
}

function createMochaConfig (mochaConfig) {
return {
client: {
mocha: mochaConfig
}
}
}