javascript - 在没有 ES6 语法和 yield 的情况下使用 mocha、Nightmare.js 进行测试

标签 javascript mocha.js nightmare

我以这个问题为例:

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/

相关文章:

javascript - 围绕一个点旋转 Canvas 并获得新的 x,y 偏移量

node.js - 什么是 Mocha 相等测试?

meteor - 使用wercker管道测试meteor js/mocha挂起

javascript - 如何在 Node 上进行 Nightmare 般的条件循环点击事件?

javascript - 将类作为参数传递给 Nightmare 评估

javascript - 如何使用 jqplot 更改饼图的圆圈大小?

javascript - 为什么默认导出被 `istanbul` 代码覆盖率列为具有多个分支?

javascript - 使用 jQuery 停止包含 SetTimeout 的循环

mocha.js - mocha : Timeout of 20000ms exceeded 单元测试错误

node.js - 如何单击此链接进入下一页? [复制]