Repository: freeCodeCamp/freeCodeCamp
File: api/src/routes/challenge.test.ts (TypeScript)

Code before:
          );
        });
      });

      describe('handling', () => {
        afterEach(async () => {
          await fastifyTestInstance.prisma.user.updateMany({
            where: { email: 'foo@bar.com' },
            data: {
              completedChallenges: [],
              savedChallenges: [],
              progressTimestamps: []
            }
          });
        });

        // HTML(0), JS(1), Modern(6), Video(11), The Odin Project(15)
        test('POST accepts challenges without files present', async () => {
          const now = Date.now();

          const response = await superRequest('/modern-challenge-completed', {
            method: 'POST',
            setCookies
          }).send(HtmlChallengeBody);

          const user = await fastifyTestInstance.prisma.user.findFirst({
            where: { email: 'foo@bar.com' }
          });

          expect(user).toMatchObject({
            completedChallenges: [
              {
                id: HtmlChallengeId,
                completedDate: expect.any(Number)
              }
            ]
          });

          const completedDate = user?.completedChallenges[0]?.completedDate;
          expect(completedDate).toBeGreaterThanOrEqual(now);
          expect(completedDate).toBeLessThanOrEqual(now + 1000);

          expect(response.statusCode).toBe(200);
          expect(response.body).toStrictEqual({
            alreadyCompleted: false,
            points: 1,
            completedDate,
            savedChallenges: []
          });
        });


Review comment (bug):
```suggestion
          const user = await fastifyTestInstance.prisma.user.findFirstOrThrow({
```
It allows us to drop some `?`s and surfaces the error earlier making for easier debugging.

Code after:
            progressTimestamps: expectedProgressTimestamps
          });

          expect(resUpdate.body).toStrictEqual({
            alreadyCompleted: true,
            points: 2,
            completedDate: expect.any(Number)
          });

          // It should return an updated completedDate
          expect(resUpdate.body.completedDate).not.toBe(
            resOriginal.body.completedDate
          );
          expect(resUpdate.statusCode).toBe(200);
        });
      });
    });

    describe('/backend-challenge-completed', () => {
      describe('validation', () => {
        test('POST rejects requests without ids', async () => {
          const response = await superRequest('/backend-challenge-completed', {
            method: 'POST',
            setCookies
          });

          expect(response.statusCode).toBe(400);
          expect(response.body).toStrictEqual(
            isValidChallengeCompletionErrorMsg
          );
        });

        test('POST rejects requests without valid ObjectIDs', async () => {
          const response = await superRequest('/backend-challenge-completed', {
            method: 'POST',
            setCookies
          }).send({ id: 'not-a-valid-id', solution: '' });

          expect(response.statusCode).toBe(400);
          expect(response.body).toStrictEqual(
            isValidChallengeCompletionErrorMsg
          );
        });
      });

      describe('handling', () => {
        afterEach(async () => {
          await fastifyTestInstance.prisma.user.updateMany({
            where: { email: 'foo@bar.com' },
            data: {
              completedChallenges: [],