configuration files (karma, protractor) to run them. Here's a sample project to show how to do that without duplicating a single piece of that configuration. Below you'll find a walk-through
Why split a test suite?
It’s a bad idea to mix both in one test suite for many reasons, one of which is that they fail for different reasons. The focused integration tests rarely fail because of bugs my team introduced.
But now we have duplication!
https://www.flickr.com/photos/popilop/331357312
|
Look at the following example, a very short snippet from a typical karma conf. Only the second entry of *files* will differ between test suites
It' a good thing that this is not only static conifiguration, but real code. If we have code then we have to power to eliminate duplication.
config.set({ ... files: [ 'src' 'unit-test/**/*.spec.js' ], // lots of other properties ... })
A solution
It is dead-simple:extract what differs to karma-unit.conf.js, and karma-integration.conf.js.
These are the entry-points and they just extract a function to build the specifics into the overwhelmingly common part.
That’s what we’ll find in buildKarmaConf.js
Here from karma-integration.conf.js
... let testFiles = 'integration-test/**/*.spec.js' let karmaConfObject = buildKarmaConf(testFiles) config.set(karmaConfObject); ...
In buildKarmaConf we have something like
function buildKarmaConf (testFiles) { var sourceFiles = ['src'] var allSourcesAndSomeTestFiles = sourceFiles.concat(testFiles) return { files: allSourcesAndSomeTestFiles, // lots of other properties ... } }
Another solution would be to use a environment variable and an if-statement instead of two *.conf.js files, but as in most of our code we have better alternatives (requiring less cyclomatic complexity), like what we're doing here - composing objects.