Skip to content

Instantly share code, notes, and snippets.

@KidkArolis
Last active October 10, 2015 21:02
Show Gist options
  • Save KidkArolis/5a0b68e2da688237cec3 to your computer and use it in GitHub Desktop.
Save KidkArolis/5a0b68e2da688237cec3 to your computer and use it in GitHub Desktop.
karma webpack --grep
module.exports = function (config) {
// karma parses cmd line args, therefore
// when executing `karma start --grep some/path`
// config will have `grep` set to 'some/path'
if (config.grep) {
var pattern = config.grep
// we then remove the .js suffix, this is useful
// because if you're typing `karma start --grep some/test_fi` and tab
// the terminal autocompletes to `karma start --grep some/test_file.js`
// however, we want to remove this to effectively make use of the
// ContextReplacementPlugin below
.replace(/\.js$/, '')
// do the samewith the Test suffix if any
.replace(/Test$/, '')
// this needs to match exactly the string you used in your
// testMain.js file in require.ensure() call
var testContext = './tests'
// add a new plugin to your webpack config
webpackConfig.plugins = (webpackConfig.plugins || []).concat([
// now we're gonna use the little known webpack plugin called
// ContextReplacementPlugin to replace the './tests' context,
// with a different regex instead of /Test$/ or /_test$/ or
// whatever you have it configured as.
new webpack.ContextReplacementPlugin(new RegExp(testContext), function (result) {
// need to do this, since webpack calls this function multiple times
// for some reason with various different objects. By checking the
// result.request, we can find the object we're looking for
if (result.request === testContext) {
// finally create an entirely new pattern for the dynamic require.ensure,
// e.g. with this new pattern executing
// $ karma start --grep tests/module/foo
// will only build those files that match: /.*tests/module/foo.*Test/
result.regExp = new RegExp('.*' + pattern + '.*Test$')
}
})
])
}
// the usual karma config setup
config.set({ /* ... */ })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment