This repository has been archived by the owner on Jul 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kir Belevich
authored
Feb 11, 2017
1 parent
671d8c4
commit ca062db
Showing
1 changed file
with
1 addition
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1 @@ | ||
## isparta instrumenter loader for [webpack](https://webpack.github.io/) | ||
|
||
[](https://www.npmjs.org/package/isparta-loader) | ||
[](https://david-dm.org/deepsweet/isparta-loader) | ||
|
||
Instrument Babel code with [isparta](https://github.com/douglasduteil/isparta) for subsequent code coverage reporting. | ||
|
||
### Install | ||
|
||
```sh | ||
$ npm i -D isparta-loader | ||
``` | ||
|
||
### Setup | ||
|
||
#### References | ||
|
||
* [Using loaders](https://webpack.github.io/docs/using-loaders.html) | ||
* [karma-webpack](https://github.com/webpack/karma-webpack#karma-webpack) | ||
* [karma-coverage](https://github.com/karma-runner/karma-coverage#configuration) | ||
|
||
#### Project structure | ||
|
||
Let's say you have the following: | ||
|
||
``` | ||
βββ src/ | ||
βΒ Β βββ components/ | ||
βΒ Β βββ bar/ | ||
βΒ Β βΒ Β βββ index.js | ||
βΒ Β βββ foo/ | ||
βΒ Β βββ index.js | ||
βββ test/ | ||
βββ src/ | ||
βββ components/ | ||
βββ foo/ | ||
βββ index.js | ||
``` | ||
|
||
To create a code coverage report for all components (even for those for which you have no tests yet) you have to require all the 1) sources and 2) tests. Something like it's described in ["alternative usage" of karma-webpack](https://github.com/webpack/karma-webpack#alternative-usage): | ||
|
||
#### test/index.js | ||
|
||
```js | ||
// require all `test/components/**/index.js` | ||
const testsContext = require.context('./src/components/', true, /index\.js$/); | ||
|
||
testsContext.keys().forEach(testsContext); | ||
|
||
// require all `src/components/**/index.js` | ||
const componentsContext = require.context('../src/components/', true, /index\.js$/); | ||
|
||
componentsContext.keys().forEach(componentsContext); | ||
``` | ||
|
||
This file will be the only entry point for Karma: | ||
|
||
#### karma.conf.js | ||
|
||
```js | ||
config.set({ | ||
β¦ | ||
files: [ | ||
'test/index.js' | ||
], | ||
preprocessors: { | ||
'test/index.js': 'webpack' | ||
}, | ||
webpack: { | ||
// *optional* babel options: isparta will use it as well as babel-loader | ||
babel: { | ||
presets: ['es2015', 'stage-0', 'react'] | ||
}, | ||
// *optional* isparta options: istanbul behind isparta will use it | ||
isparta: { | ||
embedSource: true, | ||
noAutoWrap: true, | ||
// these babel options will be passed only to isparta and not to babel-loader | ||
babel: { | ||
presets: ['es2015', 'stage-0', 'react'] | ||
} | ||
}, | ||
β¦ | ||
module: { | ||
preLoaders: [ | ||
// transpile all files except testing sources with babel as usual | ||
{ | ||
test: /\.js$/, | ||
exclude: [ | ||
path.resolve('src/components/'), | ||
path.resolve('node_modules/') | ||
], | ||
loader: 'babel' | ||
}, | ||
// transpile and instrument only testing sources with isparta | ||
{ | ||
test: /\.js$/, | ||
include: path.resolve('src/components/'), | ||
loader: 'isparta' | ||
} | ||
] | ||
} | ||
β¦ | ||
}, | ||
reporters: [ 'progress', 'coverage' ], | ||
coverageReporter: { | ||
type: 'text' | ||
}, | ||
β¦ | ||
}); | ||
``` | ||
|
||
### License | ||
[WTFPL](http://www.wtfpl.net/wp-content/uploads/2012/12/wtfpl-strip.jpg) | ||
# Package is deprecated, use [istanbul-instrumenter-loader](https://github.com/deepsweet/istanbul-instrumenter-loader) |