karma-systemjs icon indicating copy to clipboard operation
karma-systemjs copied to clipboard

How to generate coverage properly

Open Duskfall opened this issue 8 years ago • 3 comments

Hello, I am having a typescript - angular - systemjs project and I am using it with karma-systemjs. I have managed for it to work in loading the files needed properly when running karma tests, but I just cannot get it to work for karma-coverage.

karma.config.js:

module.exports = function (config) {
    config.set({
        basePath: './',
        files: [
            './Scripts/UnitTests/Tests/Controllers/**.js',
        ],
        exclude: [],
        frameworks: [
            "systemjs",
            "jasmine"
        ],
        logLevel: config.LOG_DEBUG,
        plugins: [
            'karma-coverage',
            'karma-systemjs',
            'karma-phantomjs-launcher',
            'karma-chrome-launcher',
            'karma-jasmine'
        ],
        coverageReporter: {
            type: 'json',
            subdir: '.',
            file: 'coverage-final.json'
        },
        reporters: ['progress', 'coverage'],
        preprocessors: {
            'app/**/!(*.spec).js': 'coverage'
        },
        browsers: ['Chrome'],
        autoWatch: true,
        singleRun: false,
        systemjs: {
            includeFiles: [
                './Scripts/Libs/angular/angular.min.js',
                './Scripts/Libs/angular-mocks/angular-mocks.js',
                './Scripts/Libs/lodash/dist/lodash.min.js'
            ],
            serveFiles: [
                'Scripts/Controllers/*.js',
            ],
            configFile: './system.config.js',
            config: {
                packages: {
                    'app': {
                        defaultExtension: 'js'
                    }
                }
            },
        }
    });
};

system.config.js:

System.config({
    baseURL: './',
    defaultJSExtensions: true,
    map: {
        'systemjs': 'node_modules/systemjs/dist/system.js',
        'system-polyfills': 'node_modules/systemjs/dist/system-polyfills.js',
        'typescript': "node_modules/typescript/lib/typescript.js",
        'es6-module-loader': 'node_modules/es6-module-loader/dist/es6-module-loader.js'
    },
    meta: {
        'app/*': { format: 'register' }
    },
    transpiler: "typescript"
});

if i change the preprocessors with the correct folder, for example 'Scripts/Controllers/*.js': 'coverage' I get the error DashboardController.js interpreted as global module format, but called System.register. Error loading DashboardControllerTester.js

Any suggestions?

Duskfall avatar Jul 28 '16 07:07 Duskfall

I personally haven't had any success with Typescript and code coverage. Part of the problem being that Istanbul (the tool which instruments the code to track it's coverage) needs to parse the code. By default it uses Esprima which can do ES5 (though it claims to handle ES6 too now). You can configure it to use a different parser (eg. Isparta used to be the best option for ES6), but I'm not aware of one that will work with Typescript. So the only solution I know of is to pre-compile your Typescript to Javascript before testing, but that completely defeats the point of having SystemJS call the transpiler for you.

I'm open to any bright ideas though.

rolaveric avatar Jul 28 '16 20:07 rolaveric

Hey, I understand what you are saying. Perhaps if we define as default extension the ts files and use karma-typescript-preprocessor but don't have time to test it at the moment. Thanks anyway for the fast response! :)

Duskfall avatar Jul 29 '16 07:07 Duskfall

I have been playing with the preprocessors but didn't manage to do it :/

Duskfall avatar Aug 31 '16 09:08 Duskfall