[英]Jest - TypeError: Cannot read property '[object Array]' of undefined
我用玩笑編寫了一個單元測試,並且在運行它時看到了這個錯誤。 我在控制台中獲得了正確的數據。 我已經查看了這些類似於我的帖子,但仍然不明白為什么會這樣:
這是我的測試:
import filterAvailableSlots from './filterAvailableSlots';
/* UNIT TEST */
describe('filterAvailableSlots', () => {
it('should return an array', async () => {
const bookedSlots = { '10:30': true, '11:00': true };
const allSlots = ['9:30', '10:00', '10:30', '11:00', '11:30', '12:00'];
const availableSlots = filterAvailableSlots([allSlots, bookedSlots]);
expect(Array.isArray(availableSlots)).toBe(true);
});
});
這是我的代碼:
/**
* @param {Array} allSlots
* @param {Object} bookedSlots
* @return an array of available slots
*/
export default function filterAvailableSlots(allSlots, bookedSlots) {
let availableSlots = [];
availableSlots = allSlots.filter((item) => !bookedSlots[item]);
return availableSlots;
}
應該過濾時間數組,並刪除與 bookedSlots 對象鍵匹配的任何項目。
帶有更多錯誤細節的圖像:
正確的返回值,我通過 console.log 正確看到:
[ '9:30', '10:00', '11:30', '12:00' ]
一定是我不理解的 Javascript 怪癖。 任何人都知道為什么即使我在控制台中看到了正確的數據,測試還是失敗了?
改變
const availableSlots = filterAvailableSlots([allSlots, bookedSlots]);
到
const availableSlots = filterAvailableSlots(allSlots, bookedSlots);
此外,函數中的初始let
語句沒有做任何事情。 我只想寫這個:
export default function filterAvailableSlots(allSlots, bookedSlots) {
return allSlots.filter((item) => !bookedSlots[item]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.