I've written a unit test in jest and am seeing this error when running it. I'm getting the proper data in the console. I've already reviewed these posts similar to mine, but still don't understand why it's happening:
This is my test:
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);
});
});
This is my code:
/**
* @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;
}
Should be filtering an array of times, and removing any items that match a bookedSlots object key.
Image with more error detail:
doesn't like my reference to the object property
The correct return value, which I am seeing properly via console.log:
[ '9:30', '10:00', '11:30', '12:00' ]
Must be a Javascript quirk I'm not understanding. Anyone have any idea why the test is failing even though I'm seeing the correct data in my console?
Change
const availableSlots = filterAvailableSlots([allSlots, bookedSlots]);
to
const availableSlots = filterAvailableSlots(allSlots, bookedSlots);
Also, the initial let
statement in the function isn't doing anything. I would just write this:
export default function filterAvailableSlots(allSlots, bookedSlots) {
return allSlots.filter((item) => !bookedSlots[item]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.