From 8376b8b88f377e85f4eb6a4ae1401448ba9fc81a Mon Sep 17 00:00:00 2001 From: Jonny Buchanan Date: Tue, 5 Dec 2017 20:31:07 +1000 Subject: [PATCH] Support a karma.config() function for modifying generated Karma config #408 --- CHANGES.md | 4 ++++ docs/Configuration.md | 20 ++++++++++++++++++++ src/createKarmaConfig.js | 20 ++++++++++++++++++-- tests/createKarmaConfig-test.js | 11 +++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 471f1442..930efa33 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ # Unreleased (in `master`) +## Added + +- You can now provide a [`karma.config()` function](https://github.com/insin/nwb/blob/master/docs/Configuration.md#config-function-1) which will be given the generated Karma config to do whatever it wants with [[#408](https://github.com/insin/nwb/issues/408)] + ## Dependencies - copy-webpack-plugin: v4.2.1 → [v4.2.3](https://github.com/webpack-contrib/copy-webpack-plugin/blob/master/CHANGELOG.md#423-2017-11-23) diff --git a/docs/Configuration.md b/docs/Configuration.md index 0a8792b4..d9621648 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -112,6 +112,7 @@ The configuration object can include the following properties: - [`karma.testContext`](#testcontext-string) - point to a Webpack context module for your tests - [`karma.testFiles`](#testfiles-string--arraystring) - patterns for test files - [`karma.extra`](#extra-object-1) - an escape hatch for extra Karma config, which will be merged into the generated config + - [`karma.config`](#config-function-1) - an escape hatch for manually editing the generated Karma config - [npm Build Configuration](#npm-build-configuration) - [`npm`](#npm-object) - [`npm.cjs`](#esmodules-boolean) - toggle creation of a CommonJS build @@ -1030,6 +1031,25 @@ module.exports = { } ``` +##### `config`: `Function` + +Finally, if you need *complete* control, provide a `karma.config()` function which will be given the generated config. + +> **Note:** you *must* return a config object from this function. + +```js +module.exports = { + karma: { + config(config) { + // Change config as you wish + + // You MUST return the edited config object + return config + } + } +} +``` + ### npm Build Configuration By default, nwb creates ES5 and ES6 modules builds for publishing to npm. diff --git a/src/createKarmaConfig.js b/src/createKarmaConfig.js index 1c960dbc..3773f842 100644 --- a/src/createKarmaConfig.js +++ b/src/createKarmaConfig.js @@ -4,6 +4,7 @@ import merge from 'webpack-merge' import createWebpackConfig from './createWebpackConfig' import debug from './debug' +import {UserError} from './errors' import {deepToString, typeOf} from './utils' // The following defaults are combined into a single extglob-style pattern to @@ -165,7 +166,7 @@ export default function createKarmaConfig(args, buildConfig, pluginConfig, userC ] } - let karmaConfig = merge({ + let karmaConfig = { browsers, coverageReporter: { dir: path.resolve('coverage'), @@ -208,7 +209,22 @@ export default function createKarmaConfig(args, buildConfig, pluginConfig, userC noInfo: true, quiet: true, }, - }, userKarma.extra) + } + + // Any extra user Karma config is merged into the generated config to give + // them even more control. + if (userKarma.extra) { + karmaConfig = merge(karmaConfig, userKarma.extra) + } + + // Finally, give them a chance to do whatever they want with the generated + // config. + if (typeOf(userKarma.config) === 'function') { + karmaConfig = userKarma.config(karmaConfig) + if (!karmaConfig) { + throw new UserError(`karma.config() in ${userConfig.path} didn't return anything - it must return the Karma config object.`) + } + } debug('karma config: %s', deepToString(karmaConfig)) return karmaConfig diff --git a/tests/createKarmaConfig-test.js b/tests/createKarmaConfig-test.js index fe126904..9eb4fa14 100644 --- a/tests/createKarmaConfig-test.js +++ b/tests/createKarmaConfig-test.js @@ -160,4 +160,15 @@ describe('createKarmaConfig()', () => { expect(config.browsers).toEqual(['PhantomJS', 'Chrome']) expect(config.mochaReporter).toEqual({output: 'autowatch', showDiff: true}) }) + it('supports a karma.config() function to manually edit generated config', () => { + let config = createKarmaConfig({}, {}, {}, { + karma: { + config(karmaConfig) { + karmaConfig.browsers.push('Chrome') + return karmaConfig + } + } + }) + expect(config.browsers).toEqual(['PhantomJS', 'Chrome']) + }) })