0

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?

1 Answer 1

0

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]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Of course it's a typo, blind from staring at it too long. About drive me mad!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.