Mocha 在测试时给出了太长的错误消息 node.js
Mocha give too long error message when testing node.js
我正在学习 node.js 以及如何测试功能。我在使用 mocha 时遇到问题:当函数通过测试时,一切都很好,我收到了一条漂亮的消息。
但是,如果哪个函数没有通过测试——例如,测试结果为 0,但我故意写断言期望为 1——它在 bash- 中给我一英里长的错误按摩cli 控制台:
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!代码生命周期
我正在测试的功能:(它向维基百科请求 george washington 的维基页面,并从响应中收集 html 页面。在响应读取流的 'end' 上,它解析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,它会给我一个更小和直观的错误消息。
非常感谢您的回答
如果 #requestWikiPage
中的承诺没有解决或有错误,您需要正确地拒绝它,然后在您的测试中处理该拒绝。以下更改可能会解决您问题中的问题(即让 mocha 正确处理失败的测试而无需所有额外输出),但下一步显然是让您的测试通过。
请注意,我们将拒绝回调添加到我们的 new Promise()
中,而不是下面 req.on('error'...
回调中的 console.log(err);
,我们现在使用 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);
});
});
我正在学习 node.js 以及如何测试功能。我在使用 mocha 时遇到问题:当函数通过测试时,一切都很好,我收到了一条漂亮的消息。
但是,如果哪个函数没有通过测试——例如,测试结果为 0,但我故意写断言期望为 1——它在 bash- 中给我一英里长的错误按摩cli 控制台:
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!代码生命周期
我正在测试的功能:(它向维基百科请求 george washington 的维基页面,并从响应中收集 html 页面。在响应读取流的 'end' 上,它解析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,它会给我一个更小和直观的错误消息。 非常感谢您的回答
如果 #requestWikiPage
中的承诺没有解决或有错误,您需要正确地拒绝它,然后在您的测试中处理该拒绝。以下更改可能会解决您问题中的问题(即让 mocha 正确处理失败的测试而无需所有额外输出),但下一步显然是让您的测试通过。
请注意,我们将拒绝回调添加到我们的 new Promise()
中,而不是下面 req.on('error'...
回调中的 console.log(err);
,我们现在使用 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);
});
});