首页 > 解决方案 > 开玩笑期望数组包含元素

问题描述

假设我有以下代码:

const id = '1'

const arrayOfIds = ['1', '2', '3']

我想检查id是否 arrayOfIds

像这样的东西

expect(id).toBeIn(arrayOfIds)

我该怎么做Jest

标签: javascriptjestjs

解决方案


.toContain当您要检查项目是否在数组中时使用。为了测试数组中的项目,这使用 ===,一个严格的相等检查。

test('is id in arrayOfIds', () => {
        const id = '1';
        const arrayOfIds = ['1', '2', '3'];
        expect(arrayOfIds).toContain(id);
});

推荐阅读