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

Code before:
      beforeEach(async () => {
        await fastifyTestInstance.prisma.user.updateMany({
          where: { email: developerUserEmail },
          data: {
            newEmail: null,
            emailVerified: true,
            emailVerifyTTL: null,
            emailAuthLinkTTL: null
          }
        });
      });
      test('PUT returns 200 status code with "success" message', async () => {
        const response = await superRequest('/update-my-email', {
          method: 'PUT',
          setCookies
        }).send({ email: 'foo@foo.com' });

        expect(response?.body).toEqual({
          message: 'flash.email-valid',
          type: 'success'
        });
        expect(response?.statusCode).toEqual(200);
      });

      test("PUT updates the user's record in preparation for receiving auth email", async () => {
        expect.assertions(6);
        const response = await superRequest('/update-my-email', {
          method: 'PUT',
          setCookies
        }).send({ email: unusedEmailOne });

        const user = await fastifyTestInstance.prisma.user.findFirstOrThrow({
          where: { email: developerUserEmail },
          select: { emailVerifyTTL: true, emailVerified: true, newEmail: true }
        });
        const emailVerifyTTL = user?.emailVerifyTTL;
        expect(emailVerifyTTL).toBeTruthy();
        // This throw is to mollify TS (if this is necessary a lot, create a
        // helper)
        if (!emailVerifyTTL) {
          throw new Error('emailVerifyTTL is not defined');
        }

        expect(response?.statusCode).toEqual(200);

        // expect the emailVerifyTTL to be within 10 seconds of the current time
        const tenSeconds = 10 * 1000;
        expect(emailVerifyTTL.getTime()).toBeGreaterThan(
          Date.now() - tenSeconds
        );
        expect(emailVerifyTTL.getTime()).toBeLessThan(Date.now() + tenSeconds);

Review comment (suggestion):
Since none of the `expect` statements are inside an asynchronous function, do we need the `expect.assertions`? Same goes for line 283.

Code after:
      beforeEach(async () => {
        await fastifyTestInstance.prisma.user.updateMany({
          where: { email: developerUserEmail },
          data: {
            newEmail: null,
            emailVerified: true,
            emailVerifyTTL: null,
            emailAuthLinkTTL: null
          }
        });
      });
      test('PUT returns 200 status code with "success" message', async () => {
        const response = await superRequest('/update-my-email', {
          method: 'PUT',
          setCookies
        }).send({ email: 'foo@foo.com' });

        expect(response?.body).toEqual({
          message: 'flash.email-valid',
          type: 'success'
        });
        expect(response?.statusCode).toEqual(200);
      });

      test("PUT updates the user's record in preparation for receiving auth email", async () => {
        const response = await superRequest('/update-my-email', {
          method: 'PUT',
          setCookies
        }).send({ email: unusedEmailOne });

        const user = await fastifyTestInstance.prisma.user.findFirstOrThrow({
          where: { email: developerUserEmail },
          select: { emailVerifyTTL: true, emailVerified: true, newEmail: true }
        });
        const emailVerifyTTL = user?.emailVerifyTTL;
        expect(emailVerifyTTL).toBeTruthy();
        // This throw is to mollify TS (if this is necessary a lot, create a
        // helper)
        if (!emailVerifyTTL) {
          throw new Error('emailVerifyTTL is not defined');
        }

        expect(response?.statusCode).toEqual(200);

        // expect the emailVerifyTTL to be within 10 seconds of the current time
        const tenSeconds = 10 * 1000;
        expect(emailVerifyTTL.getTime()).toBeGreaterThan(
          Date.now() - tenSeconds
        );
        expect(emailVerifyTTL.getTime()).toBeLessThan(Date.now() + tenSeconds);