简体   繁体   English

Mocha + BlanketJS + RequireJS,没有方法“ reporter”

[英]Mocha + BlanketJS + RequireJS, No method 'reporter'

I'm using Mocha with RequireJS and tests are running fine, however, when I try to add in blanket code coverage I'm getting Uncaught TypeError: Object #<HTMLDivElement> has no method 'reporter' , 我正在将Mocha与RequireJS一起使用,并且测试运行良好,但是,当我尝试添加覆盖代码范围时,我遇到了Uncaught TypeError: Object #<HTMLDivElement> has no method 'reporter'

Here's the code I'm running: 这是我正在运行的代码:

<div id="mocha"></div>

<script src="../src/js/vendor/requirejs/require.js"></script>

<script src="../src/js/vendor/blanket/dist/qunit/blanket.js"
data-cover-adapter="../src/js/vendor/blanket/src/adapters/mocha-blanket.js"></script>

<script src="SpecRunner.js" data-cover></script>

and my specrunner: 和我的specrunner:

require(["../src/js/require-config.js"], function () {

// Set testing config
require.config({
    baseUrl: "../src/js",
    paths: {
        "mocha": "vendor/mocha/mocha",
        "chai": "vendor/chai/chai"
    },
    urlArgs: "bust=" + (new Date()).getTime()
});

require([
    "require",
    "chai",
    "mocha"
], function (require, chai) {
    var should = chai.should();
    mocha.setup("bdd");

    require([
        "specs.js",
    ], function(require) {
        if (window.mochaPhantomJS) {
            mochaPhantomJS.run();
        } else {
            mocha.run();
        }
    });

});

});

Like I said - my tests are all running fine, I just can't figure out why blanket's not working. 就像我说的-我的测试都运行良好,我只是想不出为什么毯子不起作用。

Update: 更新:

I can get it to run by including the script tag for mocha at the beginning, however, now it runs the mocha tests twice. 我可以通过在开始时包含mocha的脚本标签来使其运行,但是,现在它运行了两次mocha测试。

There's a problem with how you use RequireJS. 您使用RequireJS的方式存在问题。 If you load code with RequireJS and load code with <script> tags, and: 如果使用RequireJS加载代码并使用<script>标记加载代码,并且:

  • The two sets of code are not dependent on one another, then you can load them in any order. 这两套代码彼此不依赖,因此您可以按任何顺序加载它们。

  • The code loaded with <script> depends on code loaded by RequireJS, then you should convert the code loaded with <script> to be loaded with RequireJS. <script>加载的代码取决于RequireJS加载的代码,然后您应将<script>加载的代码转换为RequireJS加载。 If you do not do this, you run into intermittent failures. 如果不这样做,则会遇到间歇性故障。

  • The code loaded with RequireJS depends on the code loaded with <script> , then the code loaded with <script> must load and execute before you start loading code with RequireJS. RequireJS加载的代码取决于<script>加载的代码,然后必须先加载并执行<script>加载的代码,然后再开始使用RequireJS加载代码。

From looking at the documentation for Blanket, I determine that your case is the second. 通过查看Blanket的文档,我确定您的情况是第二个。 You load the Blanket adapter before you start loading modules with RequireJS, but the adapter wants Mocha to be present already . 在开始使用RequireJS加载模块之前,先加载Blanket适配器,但是适配器希望Mocha 已经存在。

You will have to use a shim . 您将不得不使用垫片 I can't be certain of the exact shim you use (because I don't use blanket) but something like this should help you in the right direction: 我不确定您使用的是什么垫片(因为我不使用橡皮布),但是类似这样的方法应该可以在正确的方向上为您提供帮助:

shim: {
    "blanket": {
        exports: "blanket"
    },
    "mocha-blanket": ["mocha", "blanket"]
}

Obviously the names "blanket" and "mocha-blanket" have to be adapted to your situation. 显然, "blanket""mocha-blanket" "blanket"的名称必须适合您的情况。 I do not see a need to have an exports value on the adapter itself since the adapter attaches itself to Mocha rather than export something in the global space. 我认为适配器本身不需要具有exports值,因为适配器将自身附加到Mocha而不是在全局空间中导出某些内容。

I figured it out and did a write-up on getting Blanket to work with Mocha in AMD. 我弄清楚了,并写了一篇有关让Blanket与AMD的Mocha一起工作的文章。 Here's a blog post outlining the process as well as a repo with the working code . 这是一篇博客文章,概述了该过程以及带有工作代码回购

I'm using the following to load my tests: 我正在使用以下内容加载测试:

require(["../src/js/require-config"], function () {

  require.config({
    baseUrl: "../src/js",
    paths: {
        chai: "vendor/chai/chai"
    }
  });

  require([
    "chai"
  ], function (chai) {
    chai.should();
    window.expect = chai.expect;
    mocha.setup("bdd");

    require([
        "specs.js"
    ], function () {
        mocha.run();
    });
  });

});

And then the following code on the page: 然后页面上的以下代码:

<div id="mocha"></div>

<script src="../src/js/vendor/mocha/mocha.js"></script>

<script data-main="main-tests" src="../src/js/vendor/requirejs/require.js"></script>

<script src="../src/js/vendor/blanket/dist/qunit/blanket.js" data-cover-only="../src/js/component"></script>
<script type="text/javascript" src="../node_modules/grunt-blanket-mocha/support/mocha-blanket.js"></script>

<script>
/* global blanket */
if (window.PHANTOMJS) {
    blanket.options("reporter", "../node_modules/grunt-blanket-mocha/support/grunt-reporter.js");
}
</script>

The published mocha adapter from blanket does not works. 从毯子发布的摩卡适配器不起作用。

Install the not yet released version with bower bower install blanket#master --save-dev 使用Bower bower install blanket#master --save-dev尚未发布的版本

Also, the order of scripts inclusion matter 另外,脚本包含顺序也很重要

<script src="mocha.js"></script>
<script>mocha.setup('bdd');</script>
<script data-main="config.js" src="../bower_components/requirejs/require.js"></script>
<script src="../bower_components/blanket/dist/qunit/blanket.js" data-cover-never="bower_components"></script>
<script src="../bower_components/blanket/src/adapters/mocha-blanket.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM