From 11a0dd88e7096de6fb05576fd0bafd99e26fb62e Mon Sep 17 00:00:00 2001 From: Maksim Ryzhikov Date: Sat, 5 Nov 2016 21:17:06 +0300 Subject: [PATCH] feat: support mocha opts closes #99 --- README.md | 17 ++++++++ lib/index.js | 62 +++++++++++++++++++++++++++-- package.json | 5 ++- test/lib/index.spec.js | 89 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 166 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5dcea51..95cc3ec 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/index.js b/lib/index.js index 2b9e78d..3876b19 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,14 +1,20 @@ 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)) }) @@ -16,13 +22,61 @@ var initMocha = function (files, mochaConfig) { 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', + + '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 + } +} diff --git a/package.json b/package.json index 5ee7059..b636834 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,9 @@ "mocha" ], "author": "Vojta Jina ", - "dependencies": {}, + "dependencies": { + "minimist": "1.2.0" + }, "devDependencies": { "chai": "^3.4.1", "eslint": "^2.0.0", @@ -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" }, diff --git a/test/lib/index.spec.js b/test/lib/index.spec.js index 2ff22c4..2a882ab 100644 --- a/test/lib/index.spec.js +++ b/test/lib/index.spec.js @@ -1,3 +1,4 @@ +var mock = require('mock-fs') var expect = require('chai').expect var initMocha = require('../../lib')['framework:mocha'][1] @@ -23,7 +24,7 @@ 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') }) @@ -31,12 +32,96 @@ describe('framework:mocha', function () { 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 + } + } +}