ronantakizawa/codereview-bench huggingface dataset:
Repository: freeCodeCamp/freeCodeCamp
File: api/src/routes/user.test.ts (TypeScript)

Code before:
      });

      test('POST deletes Microsoft usernames associated with the user', async () => {
        await fastifyTestInstance.prisma.msUsername.createMany({
          data: msUsernameData
        });

        await superRequest('/account/reset-progress', {
          method: 'POST',
          setCookies
        });

        expect(await fastifyTestInstance.prisma.msUsername.count()).toBe(1);
      });

      test('POST deletes userTokens associated with the user', async () => {
        await fastifyTestInstance.prisma.userToken.createMany({
          data: tokenData
        });

        await superRequest('/account/reset-progress', {
          method: 'POST',
          setCookies
        });

        expect(await fastifyTestInstance.prisma.userToken.count()).toBe(1);
      });
    });

    describe('/user/user-token', () => {
      beforeEach(async () => {
        await fastifyTestInstance.prisma.userToken.create({
          data: {
            created: new Date(),
            id: '123',
            ttl: 1000,
            userId: defaultUserId
          }
        });
      });

      afterEach(async () => {
        await fastifyTestInstance.prisma.userToken.deleteMany({
          where: {
            userId: defaultUserId
          }
        });
      });

      // TODO(Post-MVP): consider using PUT and updating the logic to upsert
      test('POST success response includes a JWT encoded string', async () => {

Review comment (suggestion):
Wonder if we should check that the remaining item is the one that's expected on these tests that use the count.

Code after:
        expect(user?.progressTimestamps).toHaveLength(1);
        expect(user).toMatchObject(baseProgressData);
      });

      test('POST deletes Microsoft usernames associated with the user', async () => {
        await fastifyTestInstance.prisma.msUsername.createMany({
          data: msUsernameData
        });

        await superRequest('/account/reset-progress', {
          method: 'POST',
          setCookies
        });

        expect(await fastifyTestInstance.prisma.msUsername.count()).toBe(1);
      });

      test('POST deletes userTokens associated with the user', async () => {
        await fastifyTestInstance.prisma.userToken.createMany({
          data: tokenData
        });

        await superRequest('/account/reset-progress', {
          method: 'POST',
          setCookies
        });

        const userTokens =
          await fastifyTestInstance.prisma.userToken.findMany();
        expect(userTokens).toHaveLength(1);
        expect(userTokens[0]?.userId).toBe(otherUserId);
      });
    });

    describe('/user/user-token', () => {
      beforeEach(async () => {
        await fastifyTestInstance.prisma.userToken.create({
          data: {
            created: new Date(),
            id: '123',
            ttl: 1000,
            userId: defaultUserId
          }
        });
      });

      afterEach(async () => {
        await fastifyTestInstance.prisma.userToken.deleteMany({
          where: {
            userId: defaultUserId
          }
Repository: freeCodeCamp/freeCodeCamp
File: e2e/video-modal.spec.ts (TypeScript)

Code before:

test.beforeEach(async ({ page }) => {
  await page.goto(currentUrlPath);
  await page.getByTestId('get-help-dropdown').click();
  await page.getByTestId('watch-a-video').click();
});

test.afterEach(async ({ page }) => {
  await page.close();
});

test.describe('Exit Video Modal E2E Test Suite', () => {
  test('Verifies the Correct Rendering of the Video Modal', async ({
    page
  }) => {
    await expect(page.getByTestId('video-modal-title')).toBeVisible();
    await expect(
      page.getByTestId('video-modal-video-player-iframe')
    ).toBeVisible();
    await expect(
      page.getByText(translations.learn['scrimba-tip'])
    ).toBeVisible();
    await expect(
      page.getByRole('button', { name: translations.buttons.close })
    ).toBeVisible();
    await expect(page).toHaveURL(currentUrlPath);
  });

  test('Closes the Video Modal When the User clicks on exit button', async ({
    page
  }) => {
    await page
      .getByRole('button', { name: translations.buttons.close })
      .click();
    await expect(
      page.getByText(translations.buttons['watch-video'])
    ).not.toBeVisible();
    await expect(page).toHaveURL(currentUrlPath);
  });
});


Review comment (suggestion):
The modal doesn't have any logic related to URL path, so I'm not sure if this assertion is necessary.

```suggestion
```

Code after:

test.beforeEach(async ({ page }) => {
  await page.goto(currentUrlPath);
  await page.getByTestId('get-help-dropdown').click();
  await page.getByTestId('watch-a-video').click();
});

test.afterEach(async ({ page }) => {
  await page.close();
});

test.describe('Exit Video Modal E2E Test Suite', () => {
  test('Verifies the Correct Rendering of the Video Modal', async ({
    page
  }) => {
    await expect(page.getByTestId('video-modal-title')).toBeVisible();
    await expect(
      page.getByTestId('video-modal-video-player-iframe')
    ).toBeVisible();
    await expect(
      page.getByText(translations.learn['scrimba-tip'])
    ).toBeVisible();
    await expect(
      page.getByRole('button', { name: translations.buttons.close })
    ).toBeVisible();
  });

  test('Closes the Video Modal When the User clicks on exit button', async ({
    page
  }) => {
    await page
      .getByRole('button', { name: translations.buttons.close })
      .click();
    await expect(
      page.getByText(translations.buttons['watch-video'])
    ).not.toBeVisible();
  });
});
Repository: freeCodeCamp/freeCodeCamp
File: e2e/exit-exam-modal.spec.ts (TypeScript)

Code before:
import { test, expect, type Page } from '@playwright/test';
import translations from '../client/i18n/locales/english/translations.json';

test.use({ storageState: 'playwright/.auth/certified-user.json' });

let page: Page;
const examUrl =
  '/learn/foundational-c-sharp-with-microsoft/foundational-c-sharp-with-microsoft-certification-exam/foundational-c-sharp-with-microsoft-certification-exam';
const cancelExamUrl =
  '/learn/foundational-c-sharp-with-microsoft/#foundational-c-sharp-with-microsoft-certification-exam';

test.beforeAll(async ({ browser }) => {
  page = await browser.newPage();
});

test.afterAll(async () => {
  await page.close();
});

test.beforeEach(async ({ page }) => {
  await page.goto(examUrl);
  await page
    .getByRole('button', {
      name: translations.buttons['click-start-exam']
    })
    .click();
  await page
    .getByRole('button', {
      name: translations.buttons['exit-exam']
    })
    .click();
});

test.describe('Exit exam Modal E2E Test Suite', () => {
  test('Verifies the Correct Rendering of the Exit exam Modal', async ({
    page
  }) => {
    await expect(
      page.getByText(translations.learn.exam['exit-header'])
    ).toBeVisible();
    await expect(page.getByText(translations.learn.exam.exit)).toBeVisible();
    await expect(
      page.getByRole('button', {

Review comment (suggestion):
```suggestion
test.afterAll(async ({ page }) => {
  await page.close();
});
```

We should run the tests in isolation, ideally.

Since the before hook was just being used to instantiate a page, we don't need it.

Code after:
import { test, expect } from '@playwright/test';
import translations from '../client/i18n/locales/english/translations.json';

test.use({ storageState: 'playwright/.auth/certified-user.json' });

const examUrl =
  '/learn/foundational-c-sharp-with-microsoft/foundational-c-sharp-with-microsoft-certification-exam/foundational-c-sharp-with-microsoft-certification-exam';
const cancelExamUrl =
  '/learn/foundational-c-sharp-with-microsoft/#foundational-c-sharp-with-microsoft-certification-exam';

test.beforeEach(async ({ page }) => {
  await page.goto(examUrl);
  await page
    .getByRole('button', {
      name: translations.buttons['click-start-exam']
    })
    .click();
  await page
    .getByRole('button', {
      name: translations.buttons['exit-exam']
    })
    .click();
});

test.describe('Exit exam Modal E2E Test Suite', () => {
  test('Verifies the Correct Rendering of the Exit exam Modal', async ({
    page
  }) => {
    await expect(
      page.getByText(translations.learn.exam['exit-header'])
    ).toBeVisible();
    await expect(page.getByText(translations.learn.exam.exit)).toBeVisible();
    await expect(
      page.getByRole('button', {
        name: translations.learn.exam['exit-yes']
      })
    ).toBeVisible();
    await expect(
      page.getByRole('button', {
        name: translations.buttons.close
      })
    ).toBeVisible();
    await expect(
Repository: freeCodeCamp/freeCodeCamp
File: e2e/exit-exam-modal.spec.ts (TypeScript)

Code before:
import { test, expect, type Page } from '@playwright/test';
import translations from '../client/i18n/locales/english/translations.json';

test.use({ storageState: 'playwright/.auth/certified-user.json' });

let page: Page;
const examUrl =
  '/learn/foundational-c-sharp-with-microsoft/foundational-c-sharp-with-microsoft-certification-exam/foundational-c-sharp-with-microsoft-certification-exam';
const cancelExamUrl =
  '/learn/foundational-c-sharp-with-microsoft/#foundational-c-sharp-with-microsoft-certification-exam';

test.beforeAll(async ({ browser }) => {
  page = await browser.newPage();
});

test.afterAll(async () => {
  await page.close();
});

test.beforeEach(async ({ page }) => {
  await page.goto(examUrl);
  await page
    .getByRole('button', {
      name: translations.buttons['click-start-exam']
    })
    .click();
  await page
    .getByRole('button', {
      name: translations.buttons['exit-exam']
    })
    .click();

Review comment (security):
```suggestion
import { test, expect } from '@playwright/test';
import translations from '../client/i18n/locales/english/translations.json';

test.use({ storageState: 'playwright/.auth/certified-user.json' });
```

We also don't need a global `page` variable because we're already getting `page` from the test function args.

Code after:
import { test, expect } from '@playwright/test';
import translations from '../client/i18n/locales/english/translations.json';

test.use({ storageState: 'playwright/.auth/certified-user.json' });

const examUrl =
  '/learn/foundational-c-sharp-with-microsoft/foundational-c-sharp-with-microsoft-certification-exam/foundational-c-sharp-with-microsoft-certification-exam';
const cancelExamUrl =
  '/learn/foundational-c-sharp-with-microsoft/#foundational-c-sharp-with-microsoft-certification-exam';

test.beforeEach(async ({ page }) => {
  await page.goto(examUrl);
  await page
    .getByRole('button', {
      name: translations.buttons['click-start-exam']
    })
    .click();
  await page
    .getByRole('button', {
      name: translations.buttons['exit-exam']
    })
    .click();
});

test.describe('Exit exam Modal E2E Test Suite', () => {
  test('Verifies the Correct Rendering of the Exit exam Modal', async ({
    page
  }) => {
    await expect(
      page.getByText(translations.learn.exam['exit-header'])
    ).toBeVisible();
Repository: freeCodeCamp/freeCodeCamp
File: e2e/project-preview-modal.spec.ts (TypeScript)

Code before:
import { test, expect } from '@playwright/test';

import translations from '../client/i18n/locales/english/translations.json';

const currentUrlPath =
  '/learn/2022/responsive-web-design/learn-html-by-building-a-cat-photo-app/step-1';

test.beforeEach(async ({ page }) => {
  await page.goto(currentUrlPath);
});

test.afterEach(async ({ page }) => {
  await page.close();
});

test.describe('Exit Project Preview Modal E2E Test Suite', () => {
  test('Verifies the Correct Rendering of the Project Preview Modal Modal', async ({
    page
  }) => {
    await expect(
      page.getByRole('button', {
        name: translations.buttons.close
      })
    ).toBeVisible();
    await expect(page.getByTestId('project-preview-modal-title')).toBeVisible();
    await expect(
      page.locator('div.modal-body').getByTestId('preview-iframe')
    ).toBeVisible();
    //js  will generate 2 iframes, the right one should be choosen
    await expect(
      page.getByTestId('project-preview-modal-closeButton')
    ).toBeVisible();
  });

  test('Closes the Project Preview Modal When the User clicks on the close Button', async ({
    page
  }) => {
    await page.getByTestId('project-preview-modal-closeButton').click();
    await expect(
      page.getByTestId('project-preview-modal-title')
    ).not.toBeVisible();
  });

Review comment (suggestion):
```suggestion
  test('Verifies the Correct Rendering of the Project Preview Modal', async ({
```

Code after:
import { test, expect } from '@playwright/test';

import translations from '../client/i18n/locales/english/translations.json';

const currentUrlPath =
  '/learn/2022/responsive-web-design/learn-html-by-building-a-cat-photo-app/step-1';

test.beforeEach(async ({ page }) => {
  await page.goto(currentUrlPath);
});

test.afterEach(async ({ page }) => {
  await page.close();
});

test.describe('Exit Project Preview Modal E2E Test Suite', () => {
  test('Verifies the Correct Rendering of the Project Preview Modal', async ({
    page
  }) => {
    await expect(
      page.getByRole('button', {
        name: translations.buttons.close
      })
    ).toBeVisible();
    await expect(page.getByTestId('project-preview-modal-title')).toBeVisible();
    await expect(
      page.locator('div.modal-body').getByTestId('preview-iframe')
    ).toBeVisible();
    // JS will generate 2 iframes, the right one should be chosen
    await expect(
      page.getByTestId('project-preview-modal-closeButton')
    ).toBeVisible();
  });

  test('Closes the Project Preview Modal When the User clicks on the close Button', async ({
    page
  }) => {
    await page.getByTestId('project-preview-modal-closeButton').click();
    await expect(
      page.getByTestId('project-preview-modal-title')
    ).not.toBeVisible();
  });
Repository: freeCodeCamp/freeCodeCamp
File: e2e/exam-results.spec.ts (TypeScript)

Code before:
test.describe('Exam Results E2E Test Suite', () => {
  test('Verifies the Correct Rendering of the Exam results', async ({
    page
  }) => {
    await expect(page.getByTestId('exam-results-header')).toBeVisible();
    await expect(page.getByTestId('exam-results-message')).toBeVisible();
    await expect(
      page.getByTestId('exam-results-question-results')
    ).toBeVisible();
    await expect(
      page.getByTestId('exam-results-percent-results')
    ).toBeVisible();
    await expect(page.getByTestId('exam-time')).toBeVisible();
    await expect(page.getByTestId('download-exam-results')).toBeVisible();
    await expect(page.getByTestId('exit-exam')).toBeVisible();
  });

  test('Exam Results When the User clicks on Download button', async ({
    page
  }) => {
    const [downloadProm] = await Promise.all([
      page.waitForEvent('download'), // wait for download to start
      page.getByTestId('download-exam-results').click()
    ]);
    const path = await downloadProm.path();
    console.log('File was saved in ' + String(path));

    await expect(page).toHaveURL(examUrl);
  });

  test('Exam Results when the User clicks on Exit button', async ({ page }) => {
    await page.getByTestId('exit-exam').click();
    await expect(page.getByTestId('exam-results-header')).not.toBeVisible();
    await expect(page).toHaveURL(exitUrl);
  });
});


Review comment (suggestion):
We shouldn't have `console.log` in the code.

```suggestion
```

Code after:
  test('Verifies the Correct Rendering of the Exam results', async ({
    page,
    browserName
  }) => {
    test.skip(browserName === 'webkit', 'It is failing on webkit');
    await expect(
      page
        .locator('div.exam-results-wrapper')
        .getByText(
          intro['foundational-c-sharp-with-microsoft'].blocks[
            'foundational-c-sharp-with-microsoft-certification-exam'
          ].title
        )
    ).toBeVisible();
    await expect(
      page
        .locator('div.exam-results-wrapper')
        .getByText(translations.learn.exam['not-passed-message'])
    ).toBeVisible();
    await expect(
      page
        .locator('div.exam-results-wrapper')
        .getByTestId('exam-results-question-results')
    ).toBeVisible();
    await expect(
      page.locator('div.exam-results-wrapper').getByText(/\d% correct/)
    ).toBeVisible();
    await expect(
      page.locator('div.exam-results-wrapper').getByText(/Time: \d:\d/)
    ).toBeVisible();
    await expect(
      page
        .locator('div.exam-results-wrapper')
        .getByTestId('download-exam-results')
    ).toBeVisible();
    await expect(
      page
        .locator('div.exam-results-wrapper')
        .getByRole('button', { name: translations.buttons.exit })
    ).toBeVisible();
  });

  test('Exam Results when the User clicks on Exit button', async ({
    page,
    browserName
  }) => {
    test.skip(browserName === 'webkit', 'It is failing on webkit');
    await page
      .locator('div.exam-results-wrapper')
      .getByRole('button', { name: translations.buttons.exit })
      .click();
Repository: freeCodeCamp/freeCodeCamp
File: e2e/challenge-title.spec.ts (TypeScript)

Code before:
import { test, expect, type Page } from '@playwright/test';

let page: Page;

test.beforeAll(async ({ browser }) => {
  page = await browser.newPage();
  await page.goto(
    '/learn/foundational-c-sharp-with-microsoft/write-your-first-code-using-c-sharp/trophy-write-your-first-code-using-c-sharp'
  );
});

test.afterAll(async () => {
  await page.close();
});

test.describe('Challenge Title Component tests', () => {
  test('Entire Component is rendered correctly', async () => {
    await expect(page.getByTestId('challenge-title-wrap')).toBeVisible();
    await expect(page.getByTestId('challenge-title')).toBeVisible();
  });

  test('Title Displays correctly', async () => {
    const title = page.getByText('Trophy - Write Your First Code Using C#', {
      exact: true
    });
    await expect(title).toBeVisible();
  });
});


Review comment (suggestion):
Let's replace this with `beforeEach` to isolate the tests. We can access the `page` object from the callback as well:

```ts
test.beforeEach(async ({ page }) => {
  await page.goto(
    '/learn/foundational-c-sharp-with-microsoft/write-your-first-code-using-c-sharp/trophy-write-your-first-code-using-c-sharp'
  );
});
```



Code after:
import { test, expect } from '@playwright/test';
import translations from '../client/i18n/locales/english/translations.json';

test.beforeEach(async ({ page }) => {
  await page.goto(
    '/learn/information-security/python-for-penetration-testing/developing-a-port-scanner'
  );
});

test.describe('Challenge Title Component (signed out)', () => {
  test.use({ storageState: { cookies: [], origins: [] } });

  test('should render correctly', async ({ page }) => {
    await expect(page.getByLabel('Passed')).not.toBeVisible();

    await expect(
      page.getByRole('heading', { name: 'Developing a Port Scanner' })
    ).toBeVisible();
  });

  test('should not display GreenPass after challenge completion', async ({
    page
  }) => {
    // Set `force: true` to bypass Playwright's check
    // as the radio is visually hidden and the click event is intercepted by the `span` element.
    await page
      .getByRole('radio', {
        name: 'If there is an error or if no host is found, .connect() raises an exception while .connect_ex() returns an error code.'
      })
      .click({ force: true });
Repository: freeCodeCamp/freeCodeCamp
File: e2e/challenge-title.spec.ts (TypeScript)

Code before:
import { test, expect, type Page } from '@playwright/test';

let page: Page;

test.beforeAll(async ({ browser }) => {
  page = await browser.newPage();
  await page.goto(
    '/learn/foundational-c-sharp-with-microsoft/write-your-first-code-using-c-sharp/trophy-write-your-first-code-using-c-sharp'
  );
});

test.afterAll(async () => {
  await page.close();
});

test.describe('Challenge Title Component tests', () => {
  test('Entire Component is rendered correctly', async () => {
    await expect(page.getByTestId('challenge-title-wrap')).toBeVisible();
    await expect(page.getByTestId('challenge-title')).toBeVisible();
  });

  test('Title Displays correctly', async () => {
    const title = page.getByText('Trophy - Write Your First Code Using C#', {
      exact: true
    });
    await expect(title).toBeVisible();
  });
});


Review comment (suggestion):
And similarly:

```ts
test.afterEachl(async ({ page }) => {
  await page.close();
});
```

Code after:
import { test, expect } from '@playwright/test';
import translations from '../client/i18n/locales/english/translations.json';

test.beforeEach(async ({ page }) => {
  await page.goto(
    '/learn/information-security/python-for-penetration-testing/developing-a-port-scanner'
  );
});

test.describe('Challenge Title Component (signed out)', () => {
  test.use({ storageState: { cookies: [], origins: [] } });

  test('should render correctly', async ({ page }) => {
    await expect(page.getByLabel('Passed')).not.toBeVisible();

    await expect(
      page.getByRole('heading', { name: 'Developing a Port Scanner' })
    ).toBeVisible();
  });

  test('should not display GreenPass after challenge completion', async ({
    page
  }) => {
    // Set `force: true` to bypass Playwright's check
    // as the radio is visually hidden and the click event is intercepted by the `span` element.
    await page
      .getByRole('radio', {
        name: 'If there is an error or if no host is found, .connect() raises an exception while .connect_ex() returns an error code.'
      })
      .click({ force: true });

    await page
      .getByRole('button', { name: translations.buttons['check-answer'] })
      .click();

    await page
      .getByRole('button', { name: translations.buttons['go-to-next'] })