为什么我的 Nightmare 测试总是成功,即使它应该失败?

Why is my Nightmare test always successful even when it should fail?

我按照看到的教程写了这个测试。我故意要失败(点赞数),但我总是通过测试。

这是我的代码:

var Nightmare = require('nightmare');
var expect = require('chai').expect;

describe('test youtube search results', function() {

    it('check the likes', function() {
        var nightmare = Nightmare()
        nightmare
            .goto('https://www.youtube.com/watch?v=0_oPsFTyhjY')
            .scrollTo(10000,0)
            .wait('#comment-section-renderer-items')
            .evaluate(function () {
                return document.querySelector('#watch8-sentiment-actions > span > span:nth-child(1) > button > span').innerText;
            })
            .end()
            .then(function(likes) {
                expect(likes).to.equal('245'); 
            })
    });
});

你从 Nightmare 得到的是一个承诺。只是 return 测试中的承诺。并设置更高的超时时间,让 Nightmare 的时间达到 运行。所以:

it('check the likes', function() {
    this.timeout(10000); // <-- Set a higher timeout.
    var nightmare = Nightmare()
    return nightmare                 // <-- The return is here.
        .goto('https://www.youtube.com/watch?v=0_oPsFTyhjY')
        .scrollTo(10000,0)
        .wait('#comment-section-renderer-items')
        .evaluate(function () {
            return document.querySelector('#watch8-sentiment-actions > span > span:nth-child(1) > button > span').innerText;
        })
        .end()
        .then(function(likes) {
            expect(likes).to.equal('245'); 
        })
});

否则,Mocha 不知道您的测试何时真正完成。对于异步测试,您必须在测试完成时告诉 Mocha。返回承诺是实现此目的的一种方法。