Failing to setup a test environment with grunt, ph

2019-06-25 19:04发布

问题:

I'm trying to setup a test environment with grunt, phantomjs and mocha using yeoman. The problem is when run the test task I got the following warning:

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

But I'm calling mocha.run() in my index.html file. Here it is:

<!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>
      mocha.run();
    </script>
</body>
</html>

I think my problem is inside my Gruntfile, I must be missing something. Here's some of my Gruntfile:

connect: {
      test: {
        options: {
          port: 9001,
          base: [
            '.tmp',
            'test',
            '<%= yeoman.app %>'
          ]
        }
      },
...


mocha: {
  test: {
    src: ['test/*.html'],
    options: {
      urls: [ 'http://localhost:9001/test/index.html' ]
    }
  }
}

...

grunt.registerTask('test', [
  'clean:server',
  'concurrent:test',
  'autoprefixer',
  'connect:test',
  'mocha'   ]);

回答1:

I had the same error message and then I realised there are actually two bower_components directories in a yeoman setup.

$> ls -la
...
bower_components
...
test/bower_components

When I cloned a git repo of a yo generated app it has the following .gitignore file

node_modules
dist
.tmp
.sass-cache
bower_components
test/bower_components

So you need to run this twice...

bower install

Once in [root] and once in [root]/test



回答2:

Try setting mocha.test.options.run = true