Repository: freeCodeCamp/freeCodeCamp
File: api/src/exam-environment/routes/exam-environment.test.ts (TypeScript)

Code before:
            examAttempt,
            exam: userExam
          }
        });
      });
    });

    describe('GET /exam-environment/exams', () => {
      afterEach(async () => {
        // Clean up exam attempts and moderation records
        await fastifyTestInstance.prisma.examEnvironmentExamAttempt.deleteMany();
        // Reset user prerequisites
        await fastifyTestInstance.prisma.user.update({
          where: { id: defaultUserId },
          data: {
            completedChallenges: [
              { id: mock.exam.prerequisites.at(0)!, completedDate: Date.now() }
            ]
          }
        });
        // Reset exam deprecated status
        await fastifyTestInstance.prisma.examEnvironmentExam.update({
          where: { id: mock.examId },
          data: { deprecated: false }
        });
      });

      it('should return 200', async () => {
        const res = await superGet('/exam-environment/exams').set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );

        expect(res.body).toStrictEqual([
          {
            canTake: true,
            config: {
              name: mock.exam.config.name,
              note: mock.exam.config.note,
              passingPercent: mock.exam.config.passingPercent,
              totalTimeInMS: mock.exam.config.totalTimeInMS,
              retakeTimeInMS: mock.exam.config.retakeTimeInMS
            },
            id: mock.examId
          }
        ]);

        expect(res.status).toBe(200);
      });

      it('should not return any deprecated exams', async () => {

Review comment (bug):
```suggestion
      beforeEach(async () => {
        // Reset user prerequisites
        await fastifyTestInstance.prisma.user.update({
          where: { id: defaultUserId },
          data: {
            completedChallenges: [
              { id: mock.exam.prerequisites.at(0)!, completedDate: Date.now() }
            ]
          }
        });
      });

      afterEach(async () => {
        // Clean up exam attempts and moderation records
        await fastifyTestInstance.prisma.examEnvironmentExamAttempt.deleteMany();

        // Reset exam deprecated status
        await fastifyTestInstance.prisma.examEnvironmentExam.update({
          where: { id: mock.examId },
          data: { deprecated: false }
        });
      });
```
Ideally each describe block should work in isolation. Unless the prerequisites are there before the first test runs, it will fail, so I put that first.


Code after:
            examAttempt,
            exam: userExam
          }
        });
      });
    });

    describe('GET /exam-environment/exams', () => {
      beforeEach(async () => {
        // Reset user prerequisites
        await fastifyTestInstance.prisma.user.update({
          where: { id: defaultUserId },
          data: {
            completedChallenges: [
              { id: mock.exam.prerequisites.at(0)!, completedDate: Date.now() }
            ]
          }
        });
      });

      afterEach(async () => {
        // Clean up exam attempts and moderation records
        await fastifyTestInstance.prisma.examEnvironmentExamAttempt.deleteMany();

        // Reset exam deprecated status
        await fastifyTestInstance.prisma.examEnvironmentExam.update({
          where: { id: mock.examId },
          data: { deprecated: false }
        });
      });

      it('should return 200', async () => {
        const res = await superGet('/exam-environment/exams').set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );

        expect(res.body).toStrictEqual([
          {
            canTake: true,
            config: {
              name: mock.exam.config.name,
              note: mock.exam.config.note,
              passingPercent: mock.exam.config.passingPercent,
              totalTimeInMS: mock.exam.config.totalTimeInMS,
              retakeTimeInMS: mock.exam.config.retakeTimeInMS
            },
            id: mock.examId
          }
        ]);