javascript - 如何用 Jest 将两个参数测试为数字
问题描述
我有一个函数,它接收两个数字(整数),返回一个数组,其中包含(值)的第一个(数字)倍数,如 countBy(值,数字)。
例如
countBy(1, 10) returns [1, 2, 3, 4, 5, 5, 7, 8, 9, 10]
countyBy(2, 5) returns [2, 4, 6, 8, 10]
那么,应该使用什么匹配器来测试函数是否只接收数字(整数)?我做了很多测试,但仍然没有找到解决方案。
it ('Verify if are received and returned only numbers', () => {
expect(typeof countBy(2, 5)).toBe('number');
});
有人可以给我一盏灯吗?
解决方案
试试这个
const targetArray = [1, 2, 3, 4, 5, 5, 7, 8, 9, 10];
test("Verify if are received and returned only numbers", () => {
targetArray.forEach((target) => {
expect(typeof target).toBe("number");
});
});
推荐阅读
- vuetify.js - v-autocomplete @change 上一个元素
- javascript - React-native 组件不起作用,它循环并在 UI 上显示出越来越大的价值
- python - 仅当我尝试提交电子邮件表单但在本地工作正常时,我在 heroku 上收到“没有名为 django 的模块”错误
- vue.js - 如何使用 vuetify 制作自适应网站?
- android - 程序类型已经存在:android.support.customtabs.ICustomTabsCallback$Stub$Proxy
- javascript - 我应该如何正确调用 NuxtServerInit?
- android - 来自 koin 的 SharedViewModel 不能与 MapFragment 一起使用
- node.js - 本地主机到带有转发节点 js 的 Live Server
- python - 如何让 Pygame 打开图像
- python-3.x - 解决循环python中的陈旧元素