BlanketJS + Jasmine + RequireJS无代码覆盖率

时间:2015-04-23 10:30:46

标签: javascript requirejs jasmine blanket.js

我正在尝试使用与this教程相同的设置。

首先关闭。我的文件结构是:

/assests
/css
/font
/img
/js
    /collection
    /lib
    /model
    /plugin
    /spec
        -> Tests in here
    /view
    SpecRunner.js
    main.js
/templates
index.html
SpecRunner.html

我的SpecRunner.html看起来像:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Jasmine Spec Runner v2.2.0</title>

    <link rel="shortcut icon" type="image/png" href="js/lib/jasmine/jasmine_favicon.png">
    <link rel="stylesheet" href="js/lib/jasmine/jasmine.css">

    <!--
    <script type="text/javascript" src="js/lib/blanket/blanket.js"></script>
    <script type="text/javascript" src="js/lib/jasmine/jasmine.js"></script>
    <script type="text/javascript" src="js/lib/jasmine/jasmine-html.js"></script>
    <script type="text/javascript" src="js/lib/jasmine/boot.js"></script>
    <script type="text/javascript" src="js/lib/blanket/blanket_jasmine.js"></script>
    -->

    <script type="text/javascript" src="js/lib/require/require.js" data-main="js/SpecRunner.js">
    </script>   
</head>

<body>
    <!--This div is to allow the views to render. It's filled with the required garbage tags-->
    <div id="sandbox" style="overflow: hidden; height: 1px;">
        <div id="progress-bar-container">
            <div class="main_content">
            </div>
        </div>
    </div>
</body>
</html>

我的SpecRunner.js看起来像:

require.config({
  paths: {
    'jquery' : 'lib/jqm/jquery-1.11.2.min',
    'underscore' : 'lib/underscore/underscore',
    'backbone' : 'lib/backbone/backbone',
        //Testing Dependencies
        'blanket': 'lib/blanket/blanket',
        'jasmine': 'lib/jasmine/jasmine',
        'jasmine-html': 'lib/jasmine/jasmine-html',
        'jasmine-boot' : 'lib/jasmine/boot',
        'jasmine-blanket' : 'lib/blanket/blanket_jasmine'
  },
  shim: {
        backbone: {
          deps: ['underscore', 'jquery'],
          exports: 'Backbone'
        },
       'jasmine-boot' : {
            deps : [ 'jasmine', 'jasmine-html' ],
            exports : 'jasmine'
        },
        'jasmine-html' : {
            deps : [ 'jasmine' ]
        },
        'jasmine-blanket' : {
            deps : [ 'jasmine-boot', 'blanket' ],
            exports : 'blanket'
        },
    }
});

require(['jquery', 'jasmine-boot', 'jasmine-blanket', 'blanket'], function($, jasmine, blanket){
    blanket.options('filter', '../js/');
    blanket.options('antifilter', [ '../js/lib/', 
                                    '../js/plugin/', 
                                    '../js/spec/',
                                    '../js/SpecRunner.js',
                                    '../js/main.js'  ]);
    blanket.options('branchTracking', true); 

    var jasmineEnv = jasmine.getEnv();
    jasmineEnv.addReporter(new jasmine.BlanketReporter());
    jasmineEnv.updateInterval = 1000;

    var specs = [];
    specs.push('../js/spec/view/DetailView');

    $(document).ready(function() {
      require(specs, function(spec) {
        window.onload();
      });
    });   
});

问题在于,通过这个当前的设置,我到达了控制台只显示&#34;等待毯子&#34;并挂起。

我可以通过从SpecRunner.js中删除所有毯子和茉莉花依赖项来获得一个版本,并将它们放在SpecRunner.html中(即取消注释脚本引用)。但是这个版本存在缺陷,因为它没有提供任何一揽子代码覆盖。

我已经尝试了here的建议而没有运气。

任何建议都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

似乎jasmine-blanket得到了它对jasmine.getEnv()。currentRunner的引用。在Jasmine版本2.0+中未定义。控制台的最后一个输出恰好是在等待毯子......然后才打破。我用茉莉花毯替换了我的BlanketReporter的代码来解决这个问题。

var BlanketReporter = function(savePath, consolidate, useDotNotation) {

    blanket.setupCoverage();
};
BlanketReporter.finished_at = null; // will be updated after all files have been written

BlanketReporter.prototype = {
    specStarted: function(spec) {
        blanket.onTestStart();
    },

    specDone: function(result) {
        var passed = result.status === "passed" ? 1 : 0;
        blanket.onTestDone(1,passed);
    },

    jasmineDone: function() {
        blanket.onTestsDone();
    },

    log: function(str) {
        var console = jasmine.getGlobal().console;

        if (console && console.log) {
            console.log(str);
        }
    }
};

// export public
jasmine.BlanketReporter = BlanketReporter;

//override existing jasmine execute
var originalJasmineExecute = jasmine.getEnv().execute;
jasmine.getEnv().execute = function(){ console.log("waiting for blanket..."); };


blanket.beforeStartTestRunner({
    checkRequirejs:true,
    callback:function(){
        jasmine.getEnv().addReporter(new jasmine.BlanketReporter());
        jasmine.getEnv().execute = originalJasmineExecute;
        jasmine.getEnv().execute();
    }
});