我以这个问题为例:
Use Nightmare.js without ES6 syntax and yield
但是如果我把它放在 mocha 测试中,这将超时,这里是代码:
describe('Google', function() {
it('should do things', function(done) {
var x = Date.now();
var nightmare = Nightmare();
Promise.resolve(nightmare
.goto('http://google.com')
.evaluate(function() {
return document.getElementsByTagName('html')[0].innerHTML;
}))
.then(function(html) {
console.log("done in " + (Date.now()-x) + "ms");
console.log("result", html);
expect(html).to.equal('abc');
done();
return nightmare.end();
}).then(function(result) {
}, function(err) {
console.error(err); // notice that `throw`ing in here doesn't work
});
});
});
但问题是永远不会调用done()
。
最佳答案
我使用 mocha-generators 插件来做 yield。以下是我将如何构建您的代码:
require('mocha-generators').install();
var Nightmare = require('nightmare');
var expect = require('chai').expect;
describe('test login', function() {
var nightmare;
beforeEach(function *() {
nightmare = Nightmare({
show: true,
});
afterEach(function*() {
yield nightmare.end();
});
it('should go to google', function*() {
this.timeout(5000);
var result = yield nightmare
.goto('http://google.com')
.dosomeotherstuff
expect(result).to.be('something')
});
});
如果您使用生成器,则不需要 done,因为 done 是处理异步的回调
关于javascript - 在没有 ES6 语法和 yield 的情况下使用 mocha、Nightmare.js 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33739504/