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

Code before:
        await fastifyTestInstance.prisma.user.update({
          where: { id: defaultUserId },
          data: {
            completedChallenges: []
          }
        });

        const res = await superGet('/exam-environment/exams').set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );

        expect(res.body).toStrictEqual([
          {
            canTake: false,
            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);

        // Add prerequisites back to user
        await fastifyTestInstance.prisma.user.update({
          where: { id: defaultUserId },
          data: {
            completedChallenges: [
              { id: mock.exam.prerequisites.at(0)!, completedDate: Date.now() }
            ]
          }
        });

        const res2 = await superGet('/exam-environment/exams').set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );

        expect(res2.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,

Review comment (suggestion):
```suggestion
        expect(res.body).toMatchObject([{ canTake: false }]);
```
While it is worth testing the full response once, it seems more useful to keep the rest of the tests focused. In the case, we're only interested in whether or not a user can take the exam, so I'd just test that.

Code after:
      });

      it("should indicate an exam's availability based on prerequisites", async () => {
        // Remove prerequisites from user
        await fastifyTestInstance.prisma.user.update({
          where: { id: defaultUserId },
          data: {
            completedChallenges: []
          }
        });

        const res = await superGet('/exam-environment/exams').set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );

        expect(res.body).toMatchObject([{ canTake: false }]);
        expect(res.status).toBe(200);

        // Add prerequisites back to user
        await fastifyTestInstance.prisma.user.update({
          where: { id: defaultUserId },
          data: {
            completedChallenges: [
              { id: mock.exam.prerequisites.at(0)!, completedDate: Date.now() }
            ]
          }
        });

        const res2 = await superGet('/exam-environment/exams').set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );

        expect(res2.body).toMatchObject([{ canTake: true }]);
        expect(res2.status).toBe(200);
      });

      it('should indicate an exam may be taken if the user has no prior attempts', 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,