以下是JSON示例:
test = [
[
{
"name": "Test1.1",
"flow": [
{
"send": "test1.1.1",
"receive": [...]
},
{
"send": "test1.1.2",
"receive": [...]
},
]
},
{
"name": "Test1.2",
"flow": [
{
"send": "test1.2.1",
"receive": [...]
},
{
"send": "test1.2.2",
"receive": [...]
},
]
}
],
[
{
"name": "Test2.1",
"flow": [
{
"send": "test2.1.1",
"receive": [...]
},
{
"send": "test2.1.2",
"receive": [...]
},
]
},
{
"name": "Test2.2",
"flow": [
{
"send": "test2.2.1",
"receive": [...]
},
{
"send": "test2.2.2",
"receive": [...]
},
]
}
]
]
我已成功设法使用以下代码运行测试[0]
describe('Conversations', function () {
for (let test of arrayOfTests) {
describe(test.name, () => {
for (let x = 0; x < test.flow.length; x++) {
let node = test.flow[x];
it("Request: " + node.request, (done) => {
chai.request('http://localhost:5001').then(done())//WORK
});
}
});
}
});
但是,当我尝试拉入整个测试文件并逐个运行每个测试时,我开始遇到问题。我怀疑这可能是由于done()
的位置我已经厌倦了更新它,但我不完全确定如何做到这一点。
我读过有it.each
但是我不确定这对我的情况是否有帮助。
这是我尝试测试返回错误Empty test suite.
干净利落问题:如何更新以下代码以使用最顶级的JSON。
describe('Full Tests', () => {
request(
// Get testSuite
}).then((allTests) => {
for (let tests of allTests) {
describe('Single test Array', () => {
for (let test of tests) {
describe("Test: "+ test.name, () => {
for (let x = 0; x < test.flow.length; x++) {
let node = test.flow[x];
it("Request: " + node.request, (done) => {
chai.request('http://localhost:5001').then(done())//WORK
});
}
});
}
});
}
});
});
答案 0 :(得分:0)
您可以使用async/await
等待for
循环中异步任务的结果
(async(tests) => {
const done = (value) => value;
const results = [];
try {
for (let {test} of tests) {
results.push(
await Promise.all(
test.map(n =>
new Promise(resolve => {
setTimeout(
resolve
, n * 250
, {result:n * 250 < 1000
, test:`${n} * 250 < 1000`
}
)
})
)
)
.then(done))
}
} catch(err) {
throw err
}
return results
})([{
test: [1, 2, 3]
}, {
test: [4, 5, 6]
}])
.then(data => console.log(data))
.catch(err => console.error(err))