javascript - JS : use grunt + mocha + phantomjs

标签 javascript gruntjs phantomjs mocha.js

我使用 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 条目如下所示(它是生成的版本的稍微修改的版本。我用带有通配符的相对路径替换了 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>

我尝试了以下方法(没有成功):

最佳答案

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

cd test
bower install

然后再次尝试运行grunt test

您有两个 Bower_components 文件夹,一个位于 root 中,一个位于 test 中。

关于javascript - JS : use grunt + mocha + phantomjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24584394/

相关文章:

node.js - 将 phantomjs 和 casperjs 与 Nodejs 脚本一起使用?

javascript - Angularjs Bbcode 编辑器

angularjs - Grunt + Karma 测试运行程序失败并显示 "ReferenceError: module is not defined"

node.js - 如何使用全局安装的 grunt-html?

javascript - 如何确保在 Node 上使用 mocha 和 phantomjs 进行的每个测试都未触及 HTML

带有 Phantomjs 的 Python Selenium - 单击失败 : ReferenceError: Cant't find variable

javascript - CKEditor RTL问题

javascript - 使用事件监听器添加多个元素的最快方法?监听点击窗口不好吗?

$.each 语句中的 Javascript 动态变量

javascript - 通过命令行更改 Grunt 配置变量