【发布时间】:2018-12-12 20:28:24
【问题描述】:
我正在学习 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!代码生命周期
我正在测试的功能:(它向 wikipedia 请求乔治华盛顿的 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 索引上而不是在第一个索引上而收到这么长的错误消息? (实际上,我收到的每个功能都不只是这个错误消息) 如何设置 mocha 以提供更简洁和直观的错误消息。 感谢您的回答一百万
【问题讨论】:
-
在您的测试用例中设置
this.timeout(10000)并尝试。默认情况下,它有2000ms超时。 -
这就是你的意思?:describe('Async functions', function() { it('它应该返回一个html页面', (done) => { this.timeout(10000); utils.requestWikiPage().then(resBody => { assert.equal(resBody.indexOf(''), 1); done(); }); }); });
-
是的,你可以这样做......
-
抱歉仍然得到相同的错误消息,只是“错误:超过 10000 毫秒的超时......”,但感谢您的想法
-
你的承诺兑现了吗?
标签: javascript node.js testing mocha.js