JS:使用grunt + mocha + phantomjs

时间:2014-07-05 08:09:48

标签: javascript gruntjs phantomjs mocha

我使用yeoman webapp-generator创建了一个精美的网站模板。它创建了一个测试文件夹,并支持整个项目包括。一个简单的单元测试。为了尝试phantomjs功能,我添加了一个额外的功能:

describe("DOM Test", function () {

    var el = document.createElement("div");
    el.id = "myDiv";
    el.innerHTML = "Hello World!";
    document.body.appendChild(el);
    var myEl = document.getElementById('myDiv');

    it("has the right text", function () {
        (myEl.innerHTML).should.equal("Hello World!");
    });
});

但是当我运行grunt test时,我总是会遇到这个恼人的错误:

Running "mocha:test" (mocha) task
Testing: test/index.html

Warning: PhantomJS timed out, possibly due to a missing Mocha run() call. Use --force to continue.

Aborted due to warnings.

我在Gruntfile中的mocha-entry看起来像这样(它是生成的一个稍微修改过的版本。我用带通配符的相对路径替换了url):

mocha: {
      test: {
        src: ['test/**/*.html'],
      }
    },

test/index.html看起来像这样:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Mocha Spec Runner</title>
    <link rel="stylesheet" href="bower_components/mocha/mocha.css">
</head>
<body>
    <div id="mocha"></div>
    <script src="bower_components/mocha/mocha.js"></script>
    <script>mocha.setup('bdd')</script>
    <script src="bower_components/chai/chai.js"></script>
    <script>
        var assert = chai.assert;
        var expect = chai.expect;
        var should = chai.should();
    </script>

    <!-- include source files here... -->

    <!-- include spec files here... -->
    <script src="spec/test.js"></script>

    <script>
    if (window.mochaPhantomJS) { mochaPhantomJS.run(); }
      else { mocha.run(); }
    </script>
</body>
</html>

我尝试了以下事项(没有成功):

1 个答案:

答案 0 :(得分:2)

转到测试文件夹并运行bower install

cd test
bower install

然后再次尝试运行grunt test

您有两个bower_components文件夹,一个在root用户,另一个在test。