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

Code before:
          response.body.user.foobar.userToken
        ) as { userToken: string };

        expect(tokenData.id).toBe(userToken);
      });

      test('GET returns the msUsername if it exists', async () => {
        const msUsernameData = {
          msUsername: 'foobar',
          userId: defaultUserId,
          ttl: 123
        };

        await fastifyTestInstance.prisma.msUsername.createMany({
          data: msUsernameData
        });

        const msUsernames = await fastifyTestInstance.prisma.msUsername.count();
        expect(msUsernames).toBe(1);

        const response = await superGet('/user/get-session-user');

        const { msUsername } = response.body.user.foobar;

        expect(msUsername).toBe(msUsernameData.msUsername);
      });

      test('GET returns a minimal user when all optional properties are missing', async () => {
        // To get a minimal test user we first delete the existing one...
        await fastifyTestInstance.prisma.user.deleteMany({
          where: {
            email: minimalUserData.email
          }
        });
        // ...then recreate it using only the properties that the schema
        // requires. The alternative is to update, but that would require
        // a lot of unsets (this is neater)
        const testUser = await fastifyTestInstance.prisma.user.create({
          data: minimalUserData
        });

        // devLogin must not be used here since it overrides the user
        const res = await superRequest('/auth/dev-callback', { method: 'GET' });
        const setCookies = res.get('Set-Cookie');

        const publicUser = {
          ..._.omit(minimalUserData, ['externalId', 'unsubscribeId']),
          ...computedProperties,
          id: testUser.id,
          joinDate: new ObjectId(testUser.id).getTimestamp().toISOString(),
          // the following properties are defaults provided if the field is

Review comment (suggestion):
```suggestion
      test('GET returns the msUsername if it exists', async () => {
        await fastifyTestInstance.prisma.msUsername.create({
          data: msUsernameData[0] as (typeof msUsernameData)[0]
        });

        const msUsernames = await fastifyTestInstance.prisma.msUsername.count();
        expect(msUsernames).toBe(1);

        const response = await superGet('/user/get-session-user');

        const { msUsername } = response.body.user.foobar;

        expect(msUsername).toBe(msUsernameData[0]?.msUsername);
      });
```

Code after:
          response.body.user.foobar.userToken
        ) as { userToken: string };

        expect(tokenData.id).toBe(userToken);
      });

      test('GET returns the msUsername if it exists', async () => {
        await fastifyTestInstance.prisma.msUsername.create({
          data: msUsernameData[0] as (typeof msUsernameData)[0]
        });

        const msUsernames = await fastifyTestInstance.prisma.msUsername.count();
        expect(msUsernames).toBe(1);

        const response = await superGet('/user/get-session-user');

        const { msUsername } = response.body.user.foobar;

        expect(msUsername).toBe(msUsernameData[0]?.msUsername);
      });

      test('GET returns a minimal user when all optional properties are missing', async () => {
        // To get a minimal test user we first delete the existing one...
        await fastifyTestInstance.prisma.user.deleteMany({
          where: {
            email: minimalUserData.email
          }
        });
        // ...then recreate it using only the properties that the schema
        // requires. The alternative is to update, but that would require
        // a lot of unsets (this is neater)
        const testUser = await fastifyTestInstance.prisma.user.create({
          data: minimalUserData
        });

        // devLogin must not be used here since it overrides the user
        const res = await superRequest('/auth/dev-callback', { method: 'GET' });
        const setCookies = res.get('Set-Cookie');

        const publicUser = {
          ..._.omit(minimalUserData, ['externalId', 'unsubscribeId']),
          ...computedProperties,
          id: testUser.id,
          joinDate: new ObjectId(testUser.id).getTimestamp().toISOString(),
          // the following properties are defaults provided if the field is
          // missing in the user document.
          completedChallenges: [],
          completedExams: [],
          completedSurveys: [],
          partiallyCompletedChallenges: [],
          portfolio: [],