02/02/2023, 9:45 PM
Hello! I am really struggling for a few days already. I hope, someone can help me somehow or at least, point on what I am doing wrong. So, I am trying to apply some Approval Rules to GitLab (but that does not really matter). My struggle is unit testing. There are a lot of examples on testing methods that have any return value. My method just searches for the group, for the project inside the group and creates resources (Approval Rules). I do not have an idea how to test this method. My thought was to check whether the newResource callback was called and what resources did it create The code of this method is:
export async function applyRules(group: string, rules: Approval[]) {
  let groupId = await findGroupsIds([group]);
  if (groupId.length == 0) return;

  try {
    const groupProjects = await gitlab.getProjects({
      groupId: groupId[0],
      orderBy: "name",
      includeSubgroups: true,
      withShared: false,

    groupProjects.projects.forEach(async (project) => {
      const branches = await gitlab.getProjectProtectedBranches({
        projectId: project.pathWithNamespace,
      let branchIds = =>;
      const repoName = `${project.path.toLowerCase()}-${}`;
      rules.forEach((rule) => {
        const ruleName =\s/g, '-').toLowerCase();
        const projectApproval = new gitlab.ProjectApprovalRule(`${repoName}-${ruleName}`, {
          approvalsRequired: 1,
          userIds: rule.userIds,
          groupIds: rule.groupIds,
          protectedBranchIds: branchIds,
  } catch (err) {
    console.log(`Error: ${String(err).split('*').pop()}`)
Sure, the code is not perfect in some ways