Skip to content

Commit

Permalink
Support a karma.config() function for modifying generated Karma config
Browse files Browse the repository at this point in the history
  • Loading branch information
insin committed Dec 5, 2017
1 parent 4e1a8eb commit 8376b8b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
20 changes: 20 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 18 additions & 2 deletions src/createKarmaConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -165,7 +166,7 @@ export default function createKarmaConfig(args, buildConfig, pluginConfig, userC
]
}

let karmaConfig = merge({
let karmaConfig = {
browsers,
coverageReporter: {
dir: path.resolve('coverage'),
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/createKarmaConfig-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
})
})

0 comments on commit 8376b8b

Please sign in to comment.