Javascript 使用Jest编写函数测试
我有一个名为Javascript 使用Jest编写函数测试,javascript,unit-testing,jestjs,Javascript,Unit Testing,Jestjs,我有一个名为addToShoppingList的函数,它的任务是将用户输入的产品添加到购物列表中(目前只是一个数组)。我正在使用Jest进行测试,以检查我的函数是否正常工作,但我目前被卡住了: index.js const fetchData = async (product, numOfProducts) => { const res = await fetch( ... ); const data = await res.json(); return data; }; /
addToShoppingList
的函数,它的任务是将用户输入的产品添加到购物列表中(目前只是一个数组)。我正在使用Jest进行测试,以检查我的函数是否正常工作,但我目前被卡住了:
index.js
const fetchData = async (product, numOfProducts) => {
const res = await fetch( ... );
const data = await res.json();
return data;
};
// user input for products looks like 'butter 2'
// selecting Breaskstone's butter (from mockData below)
const addToShoppingList = async (product, numOfProducts = 10, getProducts = fetchData) => {
const shoppingList = [];
const item = product.slice(0 ,-2); // i.g. 'butter'
const i = +product.split(' ').pop() - 1; // i.g. '2' which becomes 1, the index number in array
const data = await getProducts(item, numOfProducts);
// i, which is the index, is used to grab specific item from list of products
let products = data.items[i].productInfo.name;
let manufacturers = data.items[i].productInfo.manufacturer;
const chosenItem = `${products} from ${manufacturer}`
shoppingList.push(chosenItem);
return shoppingList;
};
这不是很好的代码,但我正在尝试学习如何为上面的函数编写测试-我目前正在使用Jest并将其写下来,但一直收到错误消息(我一直在用谷歌搜索并试图修复)。我很想从单元测试专家那里了解我的错误之处:
index.test.js
const mockData = {
items: [
{
productInfo: {
name: 'Butter',
manufacturer: 'Land O Lakes'
},
},
{
productInfo: {
name: 'Butter',
manufacturer: 'Breakstone'
},
},
{
productInfo: {
name: 'Butter',
manufacturer: 'Kerrygold',
},
}
]
};
describe('addToShoppingList', () => {
const mockProducts = jest.fn();
mockGetBooks.mockReturnValue(mockData);
it('should call fetch with correct arguments', async () => {
await addToShoppingList('product 1', 'numOfResults', mockProducts);
expect(mockProducts).toBeCalledWith('product 1', 'numOfResults');
});
});
我想使用上面创建的
mockData
测试我的addToShoppingList
函数是否正常工作。下面是一个单元测试解决方案:
index.js
:
const fetchData=async(产品,numOfProducts)=>{
const res=等待取数('https://github.com/mrdulin');
const data=wait res.json();
返回数据;
};
//产品的用户输入看起来像“黄油2”
//选择面包石黄油(从下面的模拟数据中)
export const addToShoppingList=async(product,numOfProducts=10,getProducts=fetchData)=>{
const shoppingList=[];
const item=product.slice(0,-2);//例如“butter”
常量i=+product.split(“”).pop()-1;//i.g.'2'变成1,数组中的索引号
常量数据=等待获取产品(项目,numOfProducts);
//i是索引,用于从产品列表中获取特定项
设products=data.items[i].productInfo.name;
设manufacturers=data.items[i].productInfo.manufacturer;
const chosenItem=`products}来自${manufacturers}`;
shoppingList.push(chosenItem);
退货清单;
};
index.spec.js
:
从“.”导入{addToShoppingList};
常数mockData={
项目:[
{
产品信息:{
名字:'黄油',
制造商:'lando Lakes'
}
},
{
产品信息:{
名字:'黄油',
制造商:“碎石”
}
},
{
产品信息:{
名字:'黄油',
制造商:“Kerrygold”
}
}
]
};
描述('addToShoppingList',()=>{
它('should call fetch with correct arguments',async()=>{
const mockProducts=jest.fn().mockReturnValue(mockData);
const actualValue=等待addToShoppingList(产品1',100,模拟产品);
期望值(实际值);
预期(模拟产品)。使用(“产品”,100)调用;
});
});
单元测试结果:
PASS src/stackoverflow/58807874/index.spec.ts(8.62s)
addToShoppingList
✓ 应使用正确的参数调用fetch(10ms)
----------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
----------|----------|----------|----------|----------|-------------------|
所有文件| 70 | 40 | 33.33 | 78.57 ||
指数.ts | 70 | 40 | 33.33 | 78.57 | 2,3,4|
----------|----------|----------|----------|----------|-------------------|
测试套件:1个通过,共1个
测试:1项通过,共1项
快照:共0个
时间:10.145秒
源代码: