gpt4 book ai didi

javascript - 如何测试数组是否包含数组? ( Jest )

转载 作者:行者123 更新时间:2023-12-04 15:45:10 25 4
gpt4 key购买 nike

给定以下函数:

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(???????);
})

考虑matchers ,如何测试一个数组是否包含数组?

最佳答案

可能是这样的?

我们使用Array.isArray()(此处为docs)来测试每个元素

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();

关于javascript - 如何测试数组是否包含数组? ( Jest ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56187639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com