承诺无法通过异步等待解决

时间:2018-03-08 12:54:41

标签: javascript promise jasmine async-await jestjs

我正在尝试测试一个javascript函数,这个javascript函数是异步的,并在其中调用多个异步函数,我的问题是,当我通过测试调用此函数失败时,这是我的测试代码:

it('buildTimeslots test',async ()=>{
//INPUT DATA
const extendEndDate =  moment("2018-03-20T14:50:45.636")
const staffs = ['5a3e13125cb3df25745374c0',
'5a4471929ffd6d5fa74223b3',
'5a511e532f7de3e9ef472ecb',
'5a54b25b3dfa73ce4453a5cd',
'5a5c607f4cd740279c0a3b44',
'5a38be8b4c9f0924b96d287b',
'5a39134dc8d6a869ee8f0c4a',
'5a3948589c270a0e91e3e6b5',
'5a3a2f591157385961be6834',
'5a3a788c1157385961be683a',
'5a3a78f01157385961be683b',
'5a3a79311157385961be683c',
'5a37ae947498aa1d85ef9688',
'5a3b51aa9dbc132ad0ef9ffe',
'5a9549b9e015a91e716f77fa',
'5a9a52f59695870e7a4c3a48'];

const week = "2018-03-08";
const meetingDuration = 30;
const meetingType = [ 'application review', 'any' ];

const actual = await buildTimeslots(meetingDuration, staffs, meetingType, week,extendEndDate,{ 
      fetchCurrentWeekMeetings : fetchCurrentWeekMeetings,
      fetchCurrentWeekAvailabilityBlocks : fetchCurrentWeekAvailabilityBlocks,
      combineAvailabilityBlocks : TimeslotsHelper.combineAvailabilityBlocks,
      buildSlotHolderForWeek : TimeslotsHelper.buildSlotHolderForWeek,
      injectStaffIfAvailable : TimeslotsHelper.injectStaffIfAvailable,
      ejectStaffIfBooked : TimeslotsHelper.ejectStaffIfBooked,
      removeUnavailableSlots : TimeslotsHelper.removeUnavailableSlots
});
console.log("in test the actual is:")
console.log(actual) 
});

这是我的buildTimeslots代码:

export async function buildTimeslots(meetingDuration, staffs, meetingType, week, extendEndDate,fn) {


  const staffsIds = staffs.map(s => (s._id ? String(s._id) : String(s)));


  const availBlocks = await fn.fetchCurrentWeekAvailabilityBlocks(staffsIds, week, extendEndDate, meetingType);

   //THE FOLLOWING CONSOLE.LOG DOES NOT GET EXECUTED DUE TO TIME OUT
   console.log('buildTimeslots::availBlocks', availBlocks);

}

fn.fetchCurrentWeekAvailabilityBlocks被调用但它没有返回导致 following error

这是fetchCurrentWeekAvailabilityBlocks函数:

    export async function fetchCurrentWeekAvailabilityBlocks(
  userIds: Array<Object>,
  week: String,
  extendEndDate: moment,
  meetingType: Array<String> = ['any'],
) {
  //console.log("in fetchCurrentWeekAvailabilityBlocks, the userIds are",userIds)
  console.log("AT fetchCurrentWeekAvailabilityBlocks, userIds are: ",userIds)
  console.log("AT fetchCurrentWeekAvailabilityBlocks, week is: ",week)
  console.log("AT fetchCurrentWeekAvailabilityBlocks, extendEndDate is: ",extendEndDate)
  //mock_fetchCurrentWeekAvailabilityBlocks();
  const meetingTypeArr = Array.isArray(meetingType) ? meetingType : [ meetingType ];
  // define the date boundary for the current week
  const beginDate = moment(week)
    .startOf('week')
    .startOf('day')
    .toDate();
  const endDate = extendEndDate
    ? extendEndDate.toDate()
    : moment(week)
        .endOf('week')
        .endOf('day')
        .toDate();
  // console.log('fetchCurrentWeekAvailabilityBlocks: begin', beginDate, 'end', endDate);
  let query = {
    startTime: { $gte: beginDate, $lte: endDate },
    userId: { $in: userIds },
  };
  if (!_.isEmpty(meetingTypeArr) && !_.isEqual(meetingTypeArr, ['any'])) {
    query = { ...query, meetingType: { $in: meetingTypeArr } };
  }
  //console.log("in fetchCurrentWeekAvailabilityBlocks",AvailabilityBlock.find(query));
  //console.log("here at fetchCurrentWeekAvailabilityBlocks, found: ",AvailabilityBlock.find(query));

  return AvailabilityBlock.find(query);
}

0 个答案:

没有答案