【发布时间】:2020-07-10 14:59:34
【问题描述】:
我开玩笑地写了一个单元测试,运行它时看到这个错误。我在控制台中获得了正确的数据。我已经查看了与我类似的这些帖子,但仍然不明白为什么会这样:
这是我的测试:
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对象键的项目。
更多错误细节的图片:
doesn't like my reference to the object property
正确的返回值,我可以通过 console.log 正确看到:
[ '9:30', '10:00', '11:30', '12:00' ]
一定是我不理解的 Javascript 怪癖。即使我在控制台中看到正确的数据,任何人都知道为什么测试失败了吗?
【问题讨论】:
标签: javascript arrays jestjs javascript-objects