Skip to content

riovir/isparta-loader

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

26 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

isparta instrumenter loader for webpack

npm deps

Instrument Babel code with isparta for subsequent code coverage reporting.

Install

$ npm i -D isparta-loader

Setup

References

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:

test/index.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

var babelPresets = ['es2015', 'stage-0', 'react'];

config.set({
    …
    files: [
        'test/index.js'
    ],
    preprocessors: {
        'test/index.js': 'webpack'
    },
    webpack: {
        …
        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',
                    query: babelPresets
                },
                // transpile and instrument only testing sources with isparta
                {
                    test: /\.js$/,
                    include: path.resolve('src/components/'),
                    loader: 'isparta',
                    // *optional* isparta options: istanbul behind isparta will use it
                    query: {
                        embedSource: true,
                        noAutoWrap: true,
                        // these babel options will be passed only to isparta and not to babel-loader
                        babel: {
                            presets: babelPresets
                        }
                    }
                }
            ]
        }
        …
    },
    reporters: [ 'progress', 'coverage' ],
    coverageReporter: {
        type: 'text'
    },
    …
});

License

WTFPL

About

πŸ’― isparta instrumenter loader for webpack

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%