istanbuljs / babel-plugin-istanbul

A babel plugin that adds istanbul instrumentation to ES6 code
BSD 3-Clause "New" or "Revised" License
630 stars 75 forks source link

Support for babelPreprocessor's filename & sourceFileName #119

Open bstutsky opened 7 years ago

bstutsky commented 7 years ago

It looks like there is no support for babelPreprocessor's filename and sourceFileName. I have a project where the es6 files have extension '.es6'. When I babel before testing, I convert the es6 extension to a js extension using babelPreprocessor's filename. I have a filesystem:

TestBabelIstanbul
- src
-- basicFile.es6 <- file to be tested, in es6
- test
-- basicFile.spec.js <- test file, in es5
- Gruntfile.js 
- karma.config.js 
- package.json
- test-main.js <- requirejs setup for test

src/basicFile.js:

define( [], function() {
    return {
        addOne(num){
            return num + 1;
        },
        subOne(num){
            return num - 1;
        }
    };
} );

test/basicFile.spec.js

define(function (require) {
    describe('basicFile', function () {
        beforeEach(function () {
            this.basicFile = require("src/basicFile");
        });

        it('addOne', function () {
            expect(this.basicFile.addOne(1)).toBe(2);
            expect(this.basicFile.addOne(-1)).toBe(0);
            expect(this.basicFile.addOne(0)).toBe(1);
        });
    });
});

karma.conf.js

module.exports = function( config ) {
    config.set( {
        basePath: '',
        frameworks: [ 'jasmine', 'requirejs', 'es6-shim' ],
        files: [
            'test-main.js',
            { pattern: 'test/**/*.spec.js', included: false },
            { pattern: 'src/**/*.es6', included: false }
        ],
        exclude: [],

        babelPreprocessor: {
            options: {
                presets: [ 'es2015' ],
                sourceMap: 'inline',
                plugins: ['istanbul']
            },
            filename: function( file ) {
                return file.originalPath.replace( /\.es6$/, '.js' );
            },
            sourceFileName: function( file ) {
                return file.originalPath;
            }
        },
        preprocessors: {
            'src/**/*.es6': [ 'babel' ]
        },

        reporters: [ 'progress', 'coverage' ],
        coverageReporter: {
            includeAllSources: true,
            dir: '.coverage/',
            reporters: [
                { type: "lcov" },
                { type: "cobertura" },
                { type: 'text-summary' }
            ]
        },

        ...
    } )
};

Running karma, I get a coverage error:

24 07 2017 08:07:37.960:ERROR [coverage]: { Error: ENOENT: no such file or directory, open 'C:\XXXX\TestBabelIstanbul\src\basicFile.js' at Error (native) at Object.fs.openSync (fs.js:640:18) at Object.fs.readFileSync (fs.js:508:33) at LookupStore.get (C:\XXXX\TestBabelIstanbul\node_modules\istanbul\lib\store\fslookup.js:40:19) at HtmlReport.writeDetailPage (C:\XXXX\TestBabelIstanbul\node_modules\istanbul\lib\report\html.js:411:67) at C:\XXXX\TestBabelIstanbul\node_modules\istanbul\lib\report\html.js:489:26 at SyncFileWriter.writeFile (C:\XXXX\TestBabelIstanbul\node_modules\istanbul\lib\util\file-writer.js:57:9) at FileWriter.writeFile (C:\XXXX\TestBabelIstanbul\node_modules\istanbul\lib\util\file-writer.js:147:23) at C:\XXXX\TestBabelIstanbul\node_modules\istanbul\lib\report\html.js:488:24 at Array.forEach (native) at HtmlReport.writeFiles (C:\XXXX\TestBabelIstanbul\node_modules\istanbul\lib\report\html.js:482:23) at C:\XXXX\TestBabelIstanbul\node_modules\istanbul\lib\report\html.js:484:22 at Array.forEach (native) at HtmlReport.writeFiles (C:\XXXX\TestBabelIstanbul\node_modules\istanbul\lib\report\html.js:482:23) at HtmlReport.writeReport (C:\XXXX\TestBabelIstanbul\node_modules\istanbul\lib\report\html.js:566:14) at LcovReport.writeReport (C:\XXXX\TestBabelIstanbul\node_modules\istanbul\lib\report\lcov.js:55:19) at writeReport (C:\XXXX\TestBabelIstanbul\node_modules\karma-coverage\lib\reporter.js:68:16) at C:\XXXX\TestBabelIstanbul\node_modules\karma-coverage\lib\reporter.js:296:11 at C:\XXXX\TestBabelIstanbul\node_modules\karma\lib\helper.js:145:7 at C:\XXXX\TestBabelIstanbul\node_modules\graceful-fs\polyfills.js:287:18 at FSReqWrap.oncomplete (fs.js:123:15)

errno: -4058, code: 'ENOENT', syscall: 'open', path: 'C:\XXXX\TestBabelIstanbul\src\basicFile.js' }

However, if I change the file extension for basicFile.es6 to '.js' instead of '.es6' it works correctly. Updated karma conf:

...
        files: [
           ...,
            { pattern: 'src/**/*.js', included: false }
        ],

        // preprocess matching files before serving them to the browser
        // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
        babelPreprocessor: {
            options: {
                presets: [ 'es2015' ],
                sourceMap: 'inline',
                plugins: ['istanbul']
            }
        },
        preprocessors: {
            'src/**/*.js': [ 'babel' ]
        },
...

Is there a step I'm missing to support this extension change? If not can support be added for babelPreprocessor's filename & sourceFileName?

bstutsky commented 7 years ago

@bcoe Why do you ignore me so?