我使用 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 两次(由 this guy 建议)
- 添加了
grunt.config.set('server.port', 7002)
( suggested by this github issue post )
最佳答案
转到您的测试文件夹并运行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/