javascript – Karma-Coverage报告显示代码覆盖(显然没有涵盖)

我试图生成 HTML覆盖率报告,但它不包含我期望的输出.也许我在这里错了,但它应该只显示从spec文件调用的那些行和方法,对吧?

不知怎的,它没有.

更新:

我创建了一个存储库来提供一个工作示例,概述了问题:

https://github.com/gearsdigital/stunning-octo-train

这是我的(测试)项目设置.如果需要,我可以将它推送到GitHub仓库,因为我不知道如何设置JSfiddle来运行此代码.

TL; DR

一个生成HTML覆盖率报告的过程.此报告显示所涵盖的代码,由于没有可用的测试,因此显然没有涵盖.

karma.conf.js:

var webpack = require('webpack');
var path = require('path');

// Reference webpack.config.js,don't repeat it!
var webpackConfig = require('./webpack.config.js');

// The entry point from the referenced Webpack configuration has to be
// removed or tests will fail in weird and inscrutable ways.
// Easy enough,just define an empty entry object (null won't work).
webpackConfig.entry = {};
webpackConfig.module = {
    preLoaders: [
        {
            test: /\.js$/,// files within these directories should be excluded
            // for babel processing
            exclude: /(node_modules)/,loaders: ['babel?cacheDirectory']
        },{
            test: /\.js$/,include: /(src\/js)/,exclude: /(vendor)/,loaders: ['isparta']
        }
    ]
};

/**
 * Karma configuration
 * @param config
 */
module.exports = function (config) {
    config.set({
        browsers: ['PhantomJS'],coverageReporter: {
            dir: 'test-results',reporters: [
                {type: 'text-summary'},{type: 'html',subdir: 'coverage'}
            ]
        },files: [
            'webpack.test.config.js'
        ],frameworks: [
            'jasmine'
        ],preprocessors: {
            'webpack.test.config.js': ['webpack']
        },reporters: ['spec','coverage'],webpack: webpackConfig
    });
};

webpack.config.js:

var webpack = require('webpack');
var path = require('path');

module.exports = {
    plugins: [
        new webpack.ProvidePlugin({
            $: "jquery",jQuery: "jquery","window.jQuery": "jquery"
        })
    ]
};

webpack.test.config.js:

// make sure the file name regexp matches your test files.
var testsContext = require.context('./tests',true,/\.spec\.js$/);
testsContext.keys().forEach(testsContext);

// make sure the file name regexp matches your test files.
var srcContext = require.context('./src/js',/\.js$/);
srcContext.keys().forEach(srcContext);

bootstrap.js:

import {Calculator} from './modules/Calculator';
let c = new Calculator();
c.add(1,2); // 3

Calculator.js:

export class Calculator {
    add(op1,op2) {
        return op1 + op2;
    }

    sub(op1,op2) {
        if (typeof op1 !== 'number') {
            return false;
        }
        return op1 - op2;
    }

    mul(op1,op2) {
        return op1 * op2;
    }

    div(op1,op2) {
        return op1 / op2;
    }
}

bootstrap.spec.js:

import {Calculator} from '../src/js/modules/Calculator';

describe('Calculator',function () {

    it('should return 10',function () {
        expect(true).toBe(false);
    });

});

生成的报告:

我希望发现add(),因为它不是在任何测试中调用,而是在bootstrap.js中调用.

项目结构:

解决方法

src / js / bootstrap.js被加载;这意味着这些行被执行.
import {Calculator} from './modules/Calculator';
let c = new Calculator();
c.add(1,2); // 3

我猜这块是罪魁祸首:

{
        test: /\.js$/,loaders: ['isparta']
    }

我的代码不应该只在测试期间运用吗?

最简洁的答案是不.

导入模块时,可能会覆盖任何不在闭包/函数/方法中的内容.

// this will get coverage; because it is top-level code outside a function block
var dummy = null;

export class Calculator {
    add(op1,op2) {
        // this will get coverage only if you call explicitly the function.
        return op1 + op2;
    }
}

// this will get coverage.
var c = new Calculator();
// and Now,Calculator.add will get coverage.
c.add(1,2); // 3

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...