【问题标题】:Grunt-Mocha `PhantomJS timed out`Grunt-Mocha `PhantomJS 超时`
【发布时间】:2014-07-18 11:04:50
【问题描述】:

我在运行 grunt 的 mocha 测试时不断收到此消息。

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

测试在浏览器中运行良好,但无法从 grunt 启动。我一直在使用this Grunt 插件来运行我的浏览器测试,我的这是我的 Gruntfile.js 的相关部分。

// I've checked if it does server while my tests are running & it does
connect: {
  test: {
    // public is where all my files are, of course
    base: 'public', 
    port: 8080
  }
},

mocha: {
  client: {
    urls: ['http://0.0.0.0:8080/test.html'],
    log: true,
  }
},

这是test.html 文件

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Mocha Tests</title>
    <link rel="stylesheet" href="css/mocha.css" />
  </head>
  <body>
    <div id="mocha"></div>
    <script src="js/test.js"></script>
    <script>mocha.run();</script>
  </body>
</html>

这是public 目录

/public
- index.html
- test.html
 /css
  - main.css
  - mocha.css
 /js
  - main.js
  - test.js \\ contains main.js + mocha.js + my tests

这是 grunt 在失败后的输出。

Running "uglify:dev" (uglify) task
File public/js/main.js created.
File public/js/test.js created.

Running "connect:test" (connect) task
Started connect web server on http://0.0.0.0:8080

Running "mocha:client" (mocha) task
Testing: http://0.0.0.0:8080/test.html

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

Aborted due to warnings.

测试所需的所有 src 都放在一个文件中,其中包括我的源代码(及其依赖项)、mocha 和测试。

【问题讨论】:

    标签: javascript gruntjs phantomjs mocha.js


    【解决方案1】:

    不知道它是否仍然与您相关,但以防万一其他人遇到此问题: 就我而言,如果我直接从命令行调用它们,测试工作得非常好,比如:mocha-phantomjs .\test\src\tests.html。只是grunt-mocha 无法运行它们,抛出了这个错误。 转移到grunt-mocha-phantomjs 为我修复了它,无需更改代码。 Gruntfile 看起来像这样:

    mocha_phantomjs: {
        all : ["./test/**/*.html"]
    }
    

    (如果您在 jshint 设置中将 camelcase 设置为 true,则可能必须转义该属性名称。)

    【讨论】:

    • 我的 JSHint 设置已将 camelCase 设置为 true。如何在不更改 JSHint 设置的情况下使用此 Grunt 任务?
    • @JN11 在 mocha_phantomjs 之前添加/* jshint camelcase: false */,在上述代码中} 之后添加/* jshint camelcase: true */
    【解决方案2】:

    mocha.run() 仅在浏览器中运行测试时有效。

    要在浏览器中运行测试 grunt 这样做:

    mocha.run() 替换为:

    if (window.mochaPhantomJS) {
        mochaPhantomJS.run();
    }
    else {
        mocha.run();
    }
    

    【讨论】:

    • 对不起,我已经试过了(我猜忘了提了),不幸的是它对我没有任何改变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多