admin管理员组

文章数量:1293918

I am testing the function getAllOrdersOrderedByDateDescFindOptions in my admin-find-options.helper.ts file using Jest. The function includes ternary conditions to dynamically build a where clause based on searchOption.

Even though my test cases pass and the function executes as expected (as seen in the logs), the test coverage report shows 86.66% branch coverage instead of 100%, specifically in lines 119-132.

Function being tested (simplified):

getAllOrdersOrderedByDateDescFindOptions(
  searchOption: IPaginatedItems<ISearchOptionOrderAccountancy>,
): FindOptions<Order> {
  let test = {};
  console.log('je passe avant mon if');

  if (searchOption.items.searchByPaymentMethod?.length) {
    console.log('je passe ici');
    test = {
      '$paymentMethod.method$': {
        [Op.in]: searchOption.items.searchByPaymentMethod,
      },
    };
  }

  return {
    where: {
      [Op.and]: [
        test,
        searchOption.items.searchByService?.length
          ? { '$service.name$': { [Op.in]: searchOption.items.searchByService } }
          : {},
        searchOption.items.searchByUser
          ? { [Op.or]: [{ '$user.nom$': { [Op.like]: `%${searchOption.items.searchByUser}%` } }] }
          : {},
      ],
    },
    order: [['id', 'DESC']],
    limit: searchOption.limit,
  };
}

Logs confirm the ternary checks are being executed:

console.log(je passe avant mon if)
//Je passe avant mon if

console.log(je passe ici)
//je passe ici

Test coverage report:

src/admin/helper/admin-find-options.helper.ts | 100% Stmts | 86.66% Branch | 100% Funcs | 100% Lines | 119-132  

Why does Jest report only 86.66% branch coverage when logs show the ternary conditions are executed? How can I ensure 100% branch coverage for these ternary checks? Do I need to write additional test cases for scenarios where searchOption.items.searchByPaymentMethod is undefined or empty?

本文标签: