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

Code before:
          expect(response.statusCode).toBe(403);
        });
      });

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

        test('POST update the user savedchallenges and return them', async () => {
          const now = Date.now();
          const response = await superRequest('/save-challenge', {
            method: 'POST',
            setCookies
          }).send({
            id: multiFileCertProjectId,
            lastSavedDate: now,
            files: updatedMultiFileCertProjectBody.files
          });

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

          const savedDate = user?.savedChallenges[0]?.lastSavedDate;

          expect(user).toMatchObject({
            savedChallenges: [
              {
                id: multiFileCertProjectId,
                lastSavedDate: savedDate,
                files: updatedMultiFileCertProjectBody.files
              }
            ]
          });
          expect(response.body).toEqual({
            savedChallenges: [
              {
                id: multiFileCertProjectId,
                lastSavedDate: savedDate,
                files: updatedMultiFileCertProjectBody.files
              }
            ]
          });
          expect(user?.savedChallenges).toHaveLength(1);
          expect(response.statusCode).toBe(200);

Review comment (suggestion):
```suggestion
          const user = await fastifyTestInstance.prisma.user.findFirstOrThrow({
```
Then `user` is never `null`.

Code after:
          expect(response.statusCode).toBe(400);
        });
      });

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

        test('POST update the user savedchallenges and return them', async () => {
          const response = await superRequest('/save-challenge', {
            method: 'POST',
            setCookies
          }).send({
            id: multiFileCertProjectId,
            files: updatedMultiFileCertProjectBody.files
          });

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

          const savedDate = user.savedChallenges[0]?.lastSavedDate;

          expect(user).toMatchObject({
            savedChallenges: [
              {
                id: multiFileCertProjectId,
                lastSavedDate: savedDate,
                files: updatedMultiFileCertProjectBody.files
              }
            ]
          });
          expect(response.body).toEqual({
            savedChallenges: [
              {
                id: multiFileCertProjectId,
                lastSavedDate: savedDate,
                files: updatedMultiFileCertProjectBody.files
              }
            ]
          });
          expect(response.statusCode).toBe(200);
        });
      });
    });