1

I am using this question as example:

Use Nightmare.js without ES6 syntax and yield

But if i put it in a mocha test this will go in a timeout, here the code:

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
    });
});
});

But the problem is that done() is never called.

Community
  • 1
  • 1
fain182
  • 1,068
  • 11
  • 25

2 Answers2

2

I use mocha-generators plugin to do yield. Heres how I would structure your code:

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')
  });

});

you don't need done if you are using generators since done is a callback that handles async

Saad
  • 26,316
  • 15
  • 48
  • 69
0

You should move the end after the .end() after the evaluate otherwise you'll get many errors like done() not called and also timeout because electron process do not close.

describe('Google', function() {
   it('should do things', function(done) {
    var x = Date.now();
    var nightmare = Nightmare();
    nightmare
        .goto('http://google.com')
        .evaluate(() => {
            return document.getElementsByTagName('html')[0].innerHTML;
        })
        .end()
        .then( (html) => {
            console.log("done in " + (Date.now()-x) + "ms");
            console.log("result", html);
            expect(html).to.equal('abc');
            done();
        })
        .catch(done);
    });
});
hpfs
  • 486
  • 9
  • 14