Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Commit

Permalink
fix: coverage yields empty reports (#246)
Browse files Browse the repository at this point in the history
* fix: make use of istanbul-instrumenter-loader to get correct coverage

It is important to note that this is version 0.2.0 of the module.
Versions after that does not support the html and lcov reporters
(lcovonly is supported).

* fix: istanbul-instrumenter-loader for JS enforces pre not post

* refactor to use webpack-chain

* tweaks
  • Loading branch information
mblarsen authored and egoist committed Oct 12, 2017
1 parent ae375a2 commit 6309119
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
2 changes: 2 additions & 0 deletions packages/poi-preset-karma/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Then run `poi test`, this preset will only be activated in test mode.

Or run `poi test --watch` to run Karma in watch mode.

Or run `poi test --coverage` to get code coverage as well.

## Options

### port
Expand Down
71 changes: 48 additions & 23 deletions packages/poi-preset-karma/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,48 @@ module.exports = (options = {}) => {
poi.extendWebpack(options.extendWebpack)
}

poi.run('test', webpackConfig => {
const inferValue = (key, fallback) => {
if (typeof poi.argv[key] !== 'undefined') {
return poi.argv[key]
}
if (typeof options[key] !== 'undefined') {
return options[key]
}
return fallback
const inferValue = (key, fallback) => {
if (typeof poi.argv[key] !== 'undefined') {
return poi.argv[key]
}
if (typeof options[key] !== 'undefined') {
return options[key]
}
return fallback
}

poi.extendWebpack('test', config => {
const coverage = inferValue('coverage')

if (coverage) {
/* for general usage */
config.module.rule('istanbul-instrumenter-loader')
.test(/\.(jsx?)$/)
.exclude
.add(/(node_modules|\.test\.jsx?)/)
.end()
.pre()
.use('istanbul-instrumenter-loader')
.loader('istanbul-instrumenter-loader')
.options({
esModules: true
})

/* for vue (assumes vue-loader) */
config.module.rule('vue')
.use('vue-loader')
.tap(vueOptions => {
const instrumenterLoader = 'istanbul-instrumenter-loader?esModules=true'
vueOptions.preLoaders = (vueOptions.preLoaders || {})
vueOptions.preLoaders.js = typeof vueOptions.preLoaders.js === 'string' ?
`${vueOptions.preLoaders.js}!${instrumenterLoader}` :
instrumenterLoader
return vueOptions
})
}
})

poi.run('test', webpackConfig => {
let files = inferValue('files', ['test/unit/**/*.test.js'])
files = ensureArray(files)
files.push({ pattern: 'static/**/*', watched: false, included: false, served: true, nocache: false })
Expand All @@ -34,13 +65,12 @@ module.exports = (options = {}) => {
frameworks = ensureArray(frameworks)

const watch = inferValue('watch', false)
const coverage = inferValue('coverage')

const defaultBrowser = inferValue('headless') ? 'ChromeHeadless' : 'Chrome'
let browsers = inferValue('browsers') || defaultBrowser
browsers = ensureArray(browsers)

const coverage = inferValue('coverage')

const defaultConfig = {
port,
frameworks,
Expand All @@ -50,20 +80,15 @@ module.exports = (options = {}) => {
reporters: ['mocha'].concat(coverage ? ['coverage'] : []),
coverageReporter: {
dir: 'coverage',
reporters: [{
type: 'html',
subdir: 'report-html'
}, {
type: 'lcov',
subdir: 'report-lcov'
}]
reporters: [
{ type: 'text' },
{ type: 'html', subdir: 'report-html' },
{ type: 'lcov', subdir: 'report-lcov' }
]
},
preprocessors: files.reduce((current, next) => {
if (typeof next === 'object' && next.included !== false) {
current[next.pattern] = ['webpack', 'sourcemap']
} else if (typeof next === 'string') {
current[next] = ['webpack', 'sourcemap']
}
const key = typeof next === 'object' && next.included !== false ? next.pattern : next
current[key] = ['webpack']
return current
}, {}),
webpackMiddleware: {
Expand Down
1 change: 1 addition & 0 deletions packages/poi-preset-karma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"index.js"
],
"dependencies": {
"istanbul-instrumenter-loader": "^3.0.0",
"jasmine-core": "^2.8.0",
"karma": "^1.7.1",
"karma-chrome-launcher": "^2.2.0",
Expand Down

0 comments on commit 6309119

Please sign in to comment.