给定以下功能:
chunk = (arr, n) => {
return arr.reduce(function(p, cur, i) {
(p[i/n|0] = p[i/n|0] || []).push(cur);
return p;
}, []);
}
我想测试输出数组是否包含数组:
test("Splits a given array in N arrays", () => {
let _input = Array.from({length: 999}, () => Math.floor(Math.random() * 999));;
let int = mockService.getRandomArbitrary();
expect(generalService.chunk(_input, int)).toContain(???????);
})
考虑匹配器,如果数组包含数组,如何测试?
可能是这样的东西?
我们使用 Array.isArray()
(在此处使用文档(测试每个元素
let output = generalService.chunk(_input, int));
let containArrays = true;
if (output.length) {
output.forEach(element => {
// containArrays is false if one of the element is *not* an array
if (Array.isArray(element) === false) containArrays = false;
});
}
// if output is an empty array it doesn't contain arrays
else containArrays = false;
expect(containArrays).toBeTruthy();
如果您使用 lodash ,这很容易
_.any(arr,function(a){
return Array.isArray(a);
})