我有一个数组:
Const domesticAnimals = ['Chicken','lama','Donkey']
Const wildAnimals =['lama','lion','elephant']如何测试是否在wildAnimals数组中列出了任何domesticAnimals?
我试过了
test('test If any wild animal is in the domestic animal list', async() =>{
expect(domesticAnimals ).toContain('lama')
}) //works perfectly
test('test If any wild animal is in the domestic animal list', async() =>{
expect(domesticAnimals ).toContain(wildAnimals) // does not work
})发布于 2022-03-07 14:09:40
实际上有两个数组:
const domesticAnimals = ['Chicken','lama','Donkey'];
const wildAnimals =['lama','lion','elephant'];测试一个数组是否包含来自另一个数组的值的简单JS方法是:
domesticAnimals.some(domesticAnimal => wildAnimals.includes(domesticAnimal));因此,如果要对此进行测试,应该是:
test('test If any wild animal is in the domestic animal list', () =>{
expect(domesticAnimals.some(
domesticAnimal => wildAnimals.includes(domesticAnimal)
)).toBe(true);
});另外,您将测试函数标记为async,但实际上没有await,因此async是多余的。
https://stackoverflow.com/questions/71382024
复制相似问题