我正在学习node.js以及如何测试功能。我在使用mocha时遇到问题:当功能通过测试时,一切都很好,我得到了一条很好看的消息。
但是,如果任何未通过测试的函数(例如,测试结果为0,但我故意写出期望值为1的函数),则会在bash-cli-console中给我一英里长的错误提示:>
Async functions
(node:6001) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: 0 == 1
at utils.requestWikiPage.then.resBody (/home/sandor/Documents/learning-curve-master/node-dev-course/testing-tut/utils/utils.test.js:10:20)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:6001) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:6001) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
1) it should return a html page
0 passing (2s)
1 failing
1) Async functions
it should return a html page:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/sandor/Documents/learning-curve-master/node-dev-course/testing-tut/utils/utils.test.js)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! dev-course@1.0.0 test: `mocha ./testing-tut/**/*.test.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the dev-course@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/sandor/.npm/_logs/2018-07-04T11_31_53_292Z-debug.log
[nodemon] app crashed - waiting for file changes before starting...
我不知道为什么要得到这个部分:UnhandledPromiseRejectionWarning ... 以及为什么我要得到这部分:npm ERR!代码ELIFECYCLE
我正在测试的功能:(它向Wikipedia请求George Washington的Wiki页面,并从响应中收集html页面。在响应readstream的“末尾”,它将解析html页面。该功能仅适用于很好)
// utils.js
function requestWikiPage() {
const reqOpts = {
hostname : 'en.wikipedia.org',
port : 443,
path : '/wiki/George_Washington',
method : "GET"
}
return new Promise(resolve => {
let req = https.request(reqOpts, (res) => {
let resBody = "";
res.setEncoding('utf-8');
res.on('data', (chunk) => {
resBody += chunk;
});
res.on('end', () => {
resolve(resBody);
});
});
req.on('err', (err) => {
console.log(err);
});
req.end();
});
}
module.exports.requestWikiPage = requestWikiPage;
Mocha代码:('resBody'变量是一个字符串,包含一个html页面,其中''停留在索引0上。在断言中,我对其进行测试以创建1条错误消息)
const utils = require('./utils');
var assert = require('assert');
describe('Async functions', function() {
it('it should return a html page', (done) => {
utils.requestWikiPage().then(resBody => {
assert.equal(resBody.indexOf('<!DOCTYPE html>'), 1);
done();
});
});
});
所以我不明白为什么我会收到这么长的错误消息,只是因为我希望索引不在第一个索引上而不是0上? (实际上,我得到的错误消息是每个功能都不仅限于此) 我如何设置摩卡咖啡,它使我获得了更简单,更直观的错误消息。 感谢一百万您的答案
答案 0 :(得分:0)
如果#requestWikiPage
中的承诺无法解决或存在错误,则需要正确拒绝,然后在测试中处理该拒绝。进行以下更改很可能会解决您的问题(即让Mocha正确处理失败的测试而没有所有额外的输出),但是下一步显然是使您的测试通过。
请注意,我们将拒绝回调添加到我们的new Promise()
中,而不是下面的console.log(err);
回调中的req.on('error'...
,我们现在使用reject
作为错误回调。
// utils.js
function requestWikiPage() {
const reqOpts = {
hostname : 'en.wikipedia.org',
port : 443,
path : '/wiki/George_Washington',
method : "GET"
}
return new Promise((resolve, reject) => {
let req = https.request(reqOpts, (res) => {
let resBody = "";
res.setEncoding('utf-8');
res.on('data', (chunk) => {
resBody += chunk;
});
res.on('end', () => {
resolve(resBody);
});
});
req.on('err', reject);
req.end();
});
}
module.exports.requestWikiPage = requestWikiPage;
现在处理是否通过使用done
作为catch回调通过catch块拒绝了Promise(它将有效地将错误传递给Mocha要求的done
)。
const utils = require('./utils');
var assert = require('assert');
describe('Async functions', function() {
it('it should return a html page', (done) => {
utils.requestWikiPage().then(resBody => {
assert.equal(resBody.indexOf('<!DOCTYPE html>'), 1);
done();
}).catch(done);
});
});