首页 > 解决方案 > 如何用 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'); 
});

有人可以给我一盏灯吗?

标签: javascriptjestjs

解决方案


试试这个

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");
  });
});

推荐阅读