测试数组长度时 Mocha 测试超时
Mocha test timeouts when testing length of array
我正在用 mocha 和 expect.js 测试一个小节点模块,我有点头疼。
这是测试定义:
it('Should return an expansion array that contains all the objects that comes along within a single group', function (done) {
visibility.expand(data, config.data, companies, groups, function (error, expansion) {
expect(error).to.be(null);
expect(expansion).to.be.an('array');
expect(expansion).to.have.length(2);
expect(expansion.toString()).to.eql([testCompaniesIds[2], testCompaniesIds[3]].toString());
done();
});
});
- data 是数据库访问和功能的包装器。我在我的测试套件的 before 方法中初始化它。
- config.data 持有一个带有配置参数的对象(不相关)
- companies 是一个字符串数组
- groups 是一个字符串数组
我面临的问题是,当摩卡到达这条线时
expect(expansion).to.have.length(2);
并且长度不同于 2,而不是抛出如下错误:预期为 2 但长度为 1 它只是停止并由于超时而抛出错误。
我确认测试一直执行到该行。
更多信息:我正在测试的方法接收一组公司名称和一组组名称。每个组都包含一组公司数据库 ID。
该方法的预期行为是 return 具有相应公司 ID 的数组与属于组对象的公司 ID 数组合并。
编辑 2 由于可能重复:我错了。确实是在promise的范围内执行。
编辑 由于可能重复:期望不在承诺范围内执行(当使用承诺并在解析或拒绝函数中执行期望时,承诺会捕获错误)。
提前致谢!
像这样在 try-catch 之间包装所有测试:
it('Should return an expansion array that contains all the objects that comes along within a single group', function (done) {
visibility.expand(data, config.data, [testCompanies[0].name, testCompanies[1].name], [testGroups[0].name, testGroups[1].name], function (error, expansion) {
try {
expect(error).to.be(null);
expect(expansion).to.be.an('array');
expect(expansion).to.have.length(2);
expect(checkIdIsContainedInArray(testCompaniesIds[0], expansion)).to.be(true);
expect(checkIdIsContainedInArray(testCompaniesIds[1], expansion)).to.be(true);
expect(checkIdIsContainedInArray(testCompaniesIds[2], expansion)).to.be(true);
expect(checkIdIsContainedInArray(testCompaniesIds[3], expansion)).to.be(true);
done();
} catch (e) {
done(e);
}
});
});
此测试因数组长度(为 4,应为 2)而引发错误:
Error: expected [ '464142414441464142414441',
'464142414441464142414442',
'464142414441464142414443',
'464142414441464142414444' ] to have a length of 2 but got 4
而不是:
Error: timeout of 2000ms exceeded
这可以代表任何意思。
调试expect.js我看到它正在抛出一个错误,但 Mocha 没有设法捕获它。
虽然这个解决方案可能没有预期的那么优雅,但至少它提供了预期的反馈而不是超时。
我相信这确实按预期工作。
Mocha 测试只能在实际调用 done() 时结束异步调用。由于在 mocha 执行上下文的范围之外抛出错误,它永远不会到达该代码块并且执行基本上用完了时间。
我似乎找不到任何官方文档说明这一点,但这里有一些(希望有点相关?)参考资料 -
https://github.com/pouchdb/pouchdb/issues/1339#issuecomment-34739526
http://chaijs.com/guide/styles/#expect //搜索回调的错误处理
Is there a way to get Chai working with asynchronous Mocha tests?
Mocha failed assertion causing timeout
编辑 - 实际上,我发现我把它弄反了,它肯定会捕获异常。我会再次重新查看原始代码,看看您是否一切正确。也许更新 expect.js 并查看它是否对您当前的版本有一些问题(或提交票证)。我将 chai 与 expect 库一起使用,并且能够使其正常运行。 http://chaijs.com/
正确抛出错误的代码示例 -
var chai = require('chai')
, expect = chai.expect;
describe('something', function () {
var j = [1];
it('does something.. ', function (done) {
setInterval(function () {
console.log('its done');
expect(j).to.be.length(2);
done();
}, 1000);
});
});
//Uncaught AssertionError: expected [ 1 ] to have a length of 2 but got 1
您正在回调函数中执行预期。
这意味着代码正在visibility.expand方法中执行。
我很确定您看到的行为是因为使用了承诺...您是否在 visibility.expand 方法中使用了承诺?
我正在用 mocha 和 expect.js 测试一个小节点模块,我有点头疼。
这是测试定义:
it('Should return an expansion array that contains all the objects that comes along within a single group', function (done) {
visibility.expand(data, config.data, companies, groups, function (error, expansion) {
expect(error).to.be(null);
expect(expansion).to.be.an('array');
expect(expansion).to.have.length(2);
expect(expansion.toString()).to.eql([testCompaniesIds[2], testCompaniesIds[3]].toString());
done();
});
});
- data 是数据库访问和功能的包装器。我在我的测试套件的 before 方法中初始化它。
- config.data 持有一个带有配置参数的对象(不相关)
- companies 是一个字符串数组
- groups 是一个字符串数组
我面临的问题是,当摩卡到达这条线时
expect(expansion).to.have.length(2);
并且长度不同于 2,而不是抛出如下错误:预期为 2 但长度为 1 它只是停止并由于超时而抛出错误。
我确认测试一直执行到该行。
更多信息:我正在测试的方法接收一组公司名称和一组组名称。每个组都包含一组公司数据库 ID。
该方法的预期行为是 return 具有相应公司 ID 的数组与属于组对象的公司 ID 数组合并。
编辑 2 由于可能重复:我错了。确实是在promise的范围内执行。
编辑 由于可能重复:期望不在承诺范围内执行(当使用承诺并在解析或拒绝函数中执行期望时,承诺会捕获错误)。
提前致谢!
像这样在 try-catch 之间包装所有测试:
it('Should return an expansion array that contains all the objects that comes along within a single group', function (done) {
visibility.expand(data, config.data, [testCompanies[0].name, testCompanies[1].name], [testGroups[0].name, testGroups[1].name], function (error, expansion) {
try {
expect(error).to.be(null);
expect(expansion).to.be.an('array');
expect(expansion).to.have.length(2);
expect(checkIdIsContainedInArray(testCompaniesIds[0], expansion)).to.be(true);
expect(checkIdIsContainedInArray(testCompaniesIds[1], expansion)).to.be(true);
expect(checkIdIsContainedInArray(testCompaniesIds[2], expansion)).to.be(true);
expect(checkIdIsContainedInArray(testCompaniesIds[3], expansion)).to.be(true);
done();
} catch (e) {
done(e);
}
});
});
此测试因数组长度(为 4,应为 2)而引发错误:
Error: expected [ '464142414441464142414441',
'464142414441464142414442',
'464142414441464142414443',
'464142414441464142414444' ] to have a length of 2 but got 4
而不是:
Error: timeout of 2000ms exceeded
这可以代表任何意思。
调试expect.js我看到它正在抛出一个错误,但 Mocha 没有设法捕获它。
虽然这个解决方案可能没有预期的那么优雅,但至少它提供了预期的反馈而不是超时。
我相信这确实按预期工作。
Mocha 测试只能在实际调用 done() 时结束异步调用。由于在 mocha 执行上下文的范围之外抛出错误,它永远不会到达该代码块并且执行基本上用完了时间。
我似乎找不到任何官方文档说明这一点,但这里有一些(希望有点相关?)参考资料 -
https://github.com/pouchdb/pouchdb/issues/1339#issuecomment-34739526
http://chaijs.com/guide/styles/#expect //搜索回调的错误处理
Is there a way to get Chai working with asynchronous Mocha tests?
Mocha failed assertion causing timeout
编辑 - 实际上,我发现我把它弄反了,它肯定会捕获异常。我会再次重新查看原始代码,看看您是否一切正确。也许更新 expect.js 并查看它是否对您当前的版本有一些问题(或提交票证)。我将 chai 与 expect 库一起使用,并且能够使其正常运行。 http://chaijs.com/
正确抛出错误的代码示例 -
var chai = require('chai')
, expect = chai.expect;
describe('something', function () {
var j = [1];
it('does something.. ', function (done) {
setInterval(function () {
console.log('its done');
expect(j).to.be.length(2);
done();
}, 1000);
});
});
//Uncaught AssertionError: expected [ 1 ] to have a length of 2 but got 1
您正在回调函数中执行预期。
这意味着代码正在visibility.expand方法中执行。
我很确定您看到的行为是因为使用了承诺...您是否在 visibility.expand 方法中使用了承诺?