javascript - 开玩笑期望数组包含元素
问题描述
假设我有以下代码:
const id = '1'
const arrayOfIds = ['1', '2', '3']
我想检查id
是否在 arrayOfIds
像这样的东西
expect(id).toBeIn(arrayOfIds)
我该怎么做Jest
?
解决方案
.toContain
当您要检查项目是否在数组中时使用。为了测试数组中的项目,这使用 ===,一个严格的相等检查。
test('is id in arrayOfIds', () => {
const id = '1';
const arrayOfIds = ['1', '2', '3'];
expect(arrayOfIds).toContain(id);
});
推荐阅读
- oracle - 分类数据(如下拉数据)表格的正确设计
- html - 如何将输入数据从 html 表单发送到外部 API?
- r - 调试自定义库安装失败
- python - RuntimeError: DataLoader worker (pid 2378) 被信号杀死:Killed
- node.js - 从其他控制器从 mongodb 获取数据时获取未定义结果
- javascript - 如何在 Sharepoint 列表分组视图中隐藏列名
- github - 尝试使用 CircleCI 推送到同一存储库时出现“致命:无法分叉”
- c# - 我怎样才能使 ac# 方法更通用?
- r - R CRAN 包安装失败
- git - 这4个git.exe在Windows OS下的具体区别是什么?