akst
akst

Reputation: 956

Grunt-Mocha `PhantomJS timed out`

I keep getting this message while runnign my mocha tests from grunt.

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

The tests ran fine in the browser, however they fail to start from grunt. I've been using this Grunt plugin for running my brower tests , and my this is the relevant parts of my 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,
  }
},

Here's the test.html file

<!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>

Here's the public directory

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

Here's the output from grunt following up to the fail.

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.

All the src I need for the tests is put in a single file it includes my source (and it's dependencies), mocha, and the tests.

Upvotes: 2

Views: 3649

Answers (2)

NikhilWanpal
NikhilWanpal

Reputation: 3000

Don't know if it is still relevant to you, but just in case anyone else faces this issue: In my case, the tests worked perfectly fine if I directly called them from command line, like: mocha-phantomjs .\test\src\tests.html. It was only that grunt-mocha was unable to run them, throwing this error. Shifting to grunt-mocha-phantomjs fixed it for me, no code change required. Gruntfile looks like this:

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

(You may have to escape that property name if you have set camelcase to true in your jshint settings though.)

Upvotes: 3

singh1469
singh1469

Reputation: 2044

mocha.run() only works when running tests in the browser.

To run tests in the browser and grunt do this:

Replace mocha.run() with:

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

Upvotes: 0

Related Questions