[英]Deeply test nested return value of anonymous function from properties object with Jest
我正在用React應用程序中的Jest編碼單元測試,我想檢查一個函數是否正確地執行了它(嵌套)的工作。
該函數返回具有三個屬性的對象。 每個屬性都等於一個返回特定函數的匿名函數。 我找不到如何測試特定功能及其傳遞的參數。
這就是整個功能的樣子
const myFunction = (param) => {
return {
prop1: (value) => {
return specificFunction(param.someProp1, value);
},
prop2: (value) => {
return specificFunction(param.someProp2, value);
},
prop3: (value) => {
return specificFunction(param.someProp3, value, true);
},
};
};
我想測試
return specificFunction…
行
閱讀JestJS文檔后 ,我嘗試了一種可行的解決方案,但無法控制特定函數的傳遞參數。
describe('myFunction', () => {
it('should returns object with 3 properties that have an anonymous'
+ 'function that returns a specificFunction with specific parameters'
+ 'passed in', () => {
expect(
myFunction({
someProp1: 'a',
someProp2: 'b',
someProp3: 'c',
}),
).toEqual({
prop1: expect.any(Function),
prop2: expect.any(Function),
prop3: expect.any(Function),
});
});
});
測試通過但沒有控制
我想讓我的測試先檢查myFunction是否首先調用特定的嵌套函數,然后還要使用特定的參數(例如
describe('myFunction', () => {
it("should do the job (I don't want to write the whole (long) description)", () => {
expect(
myFunction({
someProp1: 'a',
someProp2: 'b',
someProp3: 'c',
}),
).toEqual({
prop1: // Check if specificFunction is returned from the anonymous function
//and that the first parameter is 'a' and the second is the parameter from anonymous function
prop2: // Check if specificFunction is returned from the anonymous function
// and that the first parameter is 'b' and the second is the parameter from anonymous function
prop3: // Check if specificFunction is returned from the anonymous function
// and that the first parameter is 'a', the second is the parameter from anonymous function
// and the third parameter is a boolean set to true
});
});
});
我不知道我是否正在嘗試以正確的方式解決問題。 我願意接受任何建議,但不能更改初始myFunction的邏輯
您可能想要類似的東西:
const {prop1, prop2, prop3} = myFunction({
someProp1: 'a',
someProp2: 'b',
someProp3: 'c',
})
expect(prop1(value)).toEqual(
// something here
)
expect(prop2(value)).toEqual(
// something here
)
expect(prop3(value)).toEqual(
// something here
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.