gpt4 book ai didi

node.js - 使用 Mocha 测试 NightmareJS 代码失败

转载 作者:搜寻专家 更新时间:2023-11-01 00:06:11 25 4
gpt4 key购买 nike

最近我开始学习 NodeJS 并编写了一个简单的 Youtube 抓取工具,它使用 NightmareJS 并返回每个视频 URL 的喜欢次数、观看次数、作者和标题名称。

现在我正在尝试使用 Mocha 对我的代码进行单元测试(以练习一些单元测试)并且由于某种原因它失败并出现以下错误:

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

我尝试增加超时(最多 15 秒)但没有帮助,我猜它卡在某处。我错过了什么?我也很高兴听到一些关于代码结构和实现的建设性批评。

这是我的代码:

var Nightmare = require('nightmare');
var expect = require('chai').expect;
var assert = require('chai').assert;
youtube_url = 'https://www.youtube.com/watch?v=0_oPsFTyhjY';

describe('test youtube video url results', function() {
it('should return the actual video url/title/author name/num of likes and views', function(done) {
var nightmare = Nightmare({ show: false, gotoTimeout: 3000 })
nightmare
.goto(youtube_url)
.scrollTo(10000,0)
.wait('#comment-section-renderer-items')
.evaluate(function (youtube_url) {
var authorSelector = '#watch7-user-header > div > a';
var titleSelector = '#eow-title';
var vcountSelector = '#watch7-views-info > div.watch-view-count';
var lcountSelector = '#watch8-sentiment-actions > span > span:nth-child(1) > button > span';

var authorElement = document.querySelector(authorSelector);
var titleElement = document.querySelector(titleSelector);
var vcountElement = document.querySelector(vcountSelector);
var lcountElement = document.querySelector(lcountSelector);

var JSONres = {'VIDEO URL':url, 'VIDEO TITLE': titleElement.innerText,'AUTHOR NAME': authorElement.innerText,
'NUMBER OF VIEWS': vcountElement.innerText, 'NUMBER OF LIKES': lcountElement.innerText};

return (JSONres)
},youtube_url)
.end()
.then(function (result) {
try{
expect(result['VIDEO URL']).to.equal(youtube_url);
expect(result['VIDEO TITLE']).to.equal('The Best Mouse in the World?');
expect(result['AUTHOR NAME']).to.equal('Unbox Therapy');
assert.isAtLeast(result['NUMBER OF VIEWS'], 1816808, 'The number of views is at least the number of views that has been already seen');
// It's possible to remove your like from the video so hypothetically many users may remove their likes thus there is no upper/lower
// bound on the like amount a video can have at any time except that it must be non-negative.
assert.isAtLeast(result['NUMBER OF LIKES'], 0, 'The number of likes is at least a non-negative number');
done();
}
catch(error){
done(error);
}
})

});
});

最佳答案

意外地将超时提高到 20 秒 [使用 this.timeout(20000);] 只是解决了问题(出于某种原因),尽管测试实际上从未花费更多时间少于 6 秒

关于node.js - 使用 Mocha 测试 NightmareJS 代码失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38349737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com