通过 async JavaScript 循环测试(Mocha)
Tests from looping through async JavaScript (Mocha)
我正在尝试使用 Mocha 测试异步 JavaScript,我在循环遍历异步填充的数组时遇到了一些问题。
我的目标是创建 N (=arr.length
) 个测试,每个测试对应数组的每个元素。
可能我遗漏了有关 Mocha 语义的某些内容。
到目前为止,这是我的(非工作)简化代码:
var arr = []
describe("Array test", function(){
before(function(done){
setTimeout(function(){
for(var i = 0; i < 5; i++){
arr.push(Math.floor(Math.random() * 10))
}
done();
}, 1000);
});
it('Testing elements', function(){
async.each(arr, function(el, cb){
it("testing" + el, function(done){
expect(el).to.be.a('number');
done()
})
cb()
})
})
});
我收到的输出是:
Array test
✓ Testing elements
1 passing (1s)
我想要这样的输出:
Array test
Testing elements
✓ testing3
✓ testing5
✓ testing7
✓ testing3
✓ testing1
5 passing (1s)
关于如何编写这个的任何帮助?
我得到这个工作的唯一方法有点混乱(因为它需要一个虚拟测试;原因是你不能直接将一个 it()
嵌套在另一个 it()
中,它需要 "parent" 成为 describe()
,你需要一个 it()
因为 describe()
不支持异步):
var expect = require('chai').expect;
var arr = [];
describe('Array test', function() {
before(function(done){
setTimeout(function(){
for (var i = 0; i < 5; i++){
arr.push(Math.floor(Math.random() * 10));
}
done();
}, 1000);
});
it('dummy', function(done) {
describe('Testing elements', function() {
arr.forEach(function(el) {
it('testing' + el, function(done) {
expect(el).to.be.a('number');
done();
});
});
});
done();
});
});
dummy
将最终出现在你的输出中。
我正在尝试使用 Mocha 测试异步 JavaScript,我在循环遍历异步填充的数组时遇到了一些问题。
我的目标是创建 N (=arr.length
) 个测试,每个测试对应数组的每个元素。
可能我遗漏了有关 Mocha 语义的某些内容。
到目前为止,这是我的(非工作)简化代码:
var arr = []
describe("Array test", function(){
before(function(done){
setTimeout(function(){
for(var i = 0; i < 5; i++){
arr.push(Math.floor(Math.random() * 10))
}
done();
}, 1000);
});
it('Testing elements', function(){
async.each(arr, function(el, cb){
it("testing" + el, function(done){
expect(el).to.be.a('number');
done()
})
cb()
})
})
});
我收到的输出是:
Array test
✓ Testing elements
1 passing (1s)
我想要这样的输出:
Array test
Testing elements
✓ testing3
✓ testing5
✓ testing7
✓ testing3
✓ testing1
5 passing (1s)
关于如何编写这个的任何帮助?
我得到这个工作的唯一方法有点混乱(因为它需要一个虚拟测试;原因是你不能直接将一个 it()
嵌套在另一个 it()
中,它需要 "parent" 成为 describe()
,你需要一个 it()
因为 describe()
不支持异步):
var expect = require('chai').expect;
var arr = [];
describe('Array test', function() {
before(function(done){
setTimeout(function(){
for (var i = 0; i < 5; i++){
arr.push(Math.floor(Math.random() * 10));
}
done();
}, 1000);
});
it('dummy', function(done) {
describe('Testing elements', function() {
arr.forEach(function(el) {
it('testing' + el, function(done) {
expect(el).to.be.a('number');
done();
});
});
});
done();
});
});
dummy
将最终出现在你的输出中。