Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add e2e tests #25

Merged
merged 1 commit into from
Nov 26, 2023
Merged

feat: add e2e tests #25

merged 1 commit into from
Nov 26, 2023

Conversation

levino
Copy link
Collaborator

@levino levino commented Nov 26, 2023

Summary by CodeRabbit

  • New Features

    • Implemented automated Playwright tests for the application.
    • Added GitHub Actions workflow to run Playwright tests on push or pull requests to the master branch.
  • Documentation

    • Updated .gitignore to exclude test results and reports from version control.
  • Bug Fixes

    • Ensured environment variables are correctly checked and used in API requests.
  • Chores

    • Removed the export of an unused theme from the THEMES object.
  • Tests

    • Added new Playwright test cases to verify page titles and functionality.
    • Configured Playwright to run tests in parallel and with retries on CI.
    • Set up HTML reporting for test results.

Copy link
Contributor

coderabbitai bot commented Nov 26, 2023

Walkthrough

The project has integrated Playwright for automated browser testing, with configurations tailored for CI/CD environments. Tests are set to run in parallel with retries and trace collection on failures. The .gitignore file has been updated to exclude test artifacts, and a GitHub Actions workflow has been added to automate test execution. Additionally, there's a minor update in API handling for GitHub tokens and the removal of a theme export.

Changes

File Path Change Summary
.github/workflows/playwright.yml Added GitHub Actions workflows for Playwright tests, Node.js setup, and dependency installation with pnpm.
apps/registry/.gitignore Updated to exclude test results and Playwright artifacts.
apps/registry/playwright.config.js Configured Playwright for CI with parallel execution, retries, and specific browser projects.
apps/registry/tests/... Added new Playwright test cases to check web page titles.
apps/registry/pages/api/[payload].js Added conditional GitHub token handling for API requests.
apps/registry/pages/api/formatters/template.js Removed the export of the apage theme.

🐇✨
In the code's burrow, deep and vast,
Playwright's magic is cast at last.
Tests in browsers, running free,
A CI dance of code and glee. 🎭🚀


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link

vercel bot commented Nov 26, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jsonresume-org-homepage ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 26, 2023 7:46pm
jsonresume-org-registry ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 26, 2023 7:46pm

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b9c862d and 3202f2e.
Files ignored due to filter (3)
  • apps/registry/package.json
  • pnpm-lock.yaml
  • turbo.json
Files selected for processing (4)
  • .github/workflows/playwright.yml (1 hunks)
  • apps/registry/.gitignore (1 hunks)
  • apps/registry/playwright.config.js (1 hunks)
  • apps/registry/tests/render.spec.js (1 hunks)
Files skipped from review due to trivial changes (1)
  • apps/registry/.gitignore
Additional comments: 7
.github/workflows/playwright.yml (3)
  • 17-17:
    Verify if pnpm is pre-installed on GitHub Actions runners. If it is, the command npm install -g pnpm is redundant and can be removed to save time and resources.
- run: npm install -g pnpm && pnpm install
+ run: pnpm install
  • 22-22:
    Verify that the project uses Turbo and that the test:e2e script is correctly defined in the package.json to run the Playwright tests.

  • 28-28:
    Consider if a retention period of 30 days for test artifacts is necessary. If not, reducing this value can help save storage space on GitHub.

apps/registry/playwright.config.js (3)
  • 8-8:
    Verify whether the environment variables should be loaded from a .env file or if this is an intentional change.

  • 33-33:
    Verify whether the baseURL should be set for the tests or if this is an intentional change.

  • 57-74:
    Verify whether the mobile and branded browser tests are out of scope for this configuration or if they should be included.

apps/registry/tests/render.spec.js (1)
  • 8-8:

The use of a regular expression in the toHaveTitle assertion is appropriate for flexibility in matching the title.

Comment on lines +1 to +9
// @ts-check
const { test, expect } = require('@playwright/test');

test('has title', async ({ page }) => {
await page.goto('http://localhost:3002/thomasdavis');

// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(/Thomas Davis/);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using environment variables or a configuration file for the base URL (http://localhost:3002) to ensure the test is environment-agnostic.

// @ts-check
const { test, expect } = require('@playwright/test');

test('has title', async ({ page }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name "has title" could be more descriptive, such as "Page title contains 'Thomas Davis'". This would provide more context when looking at test results.

@levino
Copy link
Collaborator Author

levino commented Nov 26, 2023

@thomasdavis Please see 89af6f7. The apage theme requires an environment variable LANG to be present, which is not the case on MacOS, but probably only on linux. I think that makes the theme essentially broken and we should remove it, until this issue has been fixed. Opened #27 for this.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b9c862d and 89af6f7.
Files ignored due to filter (3)
  • apps/registry/package.json
  • pnpm-lock.yaml
  • turbo.json
Files selected for processing (5)
  • .github/workflows/playwright.yml (1 hunks)
  • apps/registry/.gitignore (1 hunks)
  • apps/registry/pages/api/formatters/template.js (1 hunks)
  • apps/registry/playwright.config.js (1 hunks)
  • apps/registry/tests/render.spec.js (1 hunks)
Files skipped from review due to trivial changes (2)
  • apps/registry/.gitignore
  • apps/registry/pages/api/formatters/template.js
Additional comments: 10
.github/workflows/playwright.yml (1)
  • 24-24:
    Verify the necessity and permissions of the custom LEVINS_TOKEN and consider using the default GITHUB_TOKEN provided by GitHub Actions if it suffices for the workflow's requirements.
    [SECURITY]
apps/registry/playwright.config.js (8)
  • 8-8:
    The dotenv package is commented out, which may prevent environment variables from being loaded. Verify if this is intentional or an oversight.

  • 17-17:
    The reuseExistingServer property is correctly configured to depend on the CI environment variable, ensuring that existing servers are reused in local development but not in CI environments.

  • 23-23:
    The forbidOnly property is correctly configured to prevent accidental commits of .only in test suites when running in CI environments.

  • 25-25:
    The retries property is correctly configured to allow retries in CI environments, which can help mitigate flakiness in end-to-end tests.

  • 27-27:
    The workers property is set to 1 in CI environments, which contradicts the summary's mention of full parallel test execution. Clarify whether this is intentional or a misconfiguration.

  • 29-29:
    The reporter property is set to 'html', which aligns with the summary and provides a visual report of test results.

  • 36-36:
    The trace property is set to 'on-first-retry', which is a useful setting for capturing additional debugging information on test retries.

  • 40-54:
    The projects for Chromium, Firefox, and WebKit are correctly configured with device-specific settings, ensuring tests are run on major browsers as intended.

apps/registry/tests/render.spec.js (1)
  • 4-9:

The test case name "has title" could be more descriptive, such as "Page title contains 'Thomas Davis'". This would provide more context when looking at test results. Additionally, consider using environment variables or a configuration file for the base URL (http://localhost:3002) to ensure the test is environment-agnostic. Lastly, adding comments or documentation explaining the purpose of the test or the significance of the URL could improve maintainability.

with:
node-version: 18
- name: Install dependencies
run: npm install -g pnpm && pnpm install
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a more direct method to install pnpm that does not involve modifying the global npm installation, if possible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of complaining, you could rather just propose a change to fix this problem...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@levino My apologies for any confusion. Here's a proposed change to the workflow file that uses corepack to enable pnpm without global installation:

@@ -0,0 +1,30 @@
 name: Playwright Tests
 on:
   push:
     branches: [master]
   pull_request:
     branches: [master]
 jobs:
   test:
     timeout-minutes: 60
     runs-on: ubuntu-latest
     steps:
       - uses: actions/checkout@v3
       - uses: actions/setup-node@v3
         with:
           node-version: 18
+          corepack: true
       - name: Install dependencies
-        run: npm install -g pnpm && pnpm install
+        run: corepack enable && pnpm install

This change enables corepack which is a tool that comes with Node.js to manage package managers like pnpm, yarn, etc., and allows you to use pnpm without installing it globally.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b9c862d and 0f1755a.
Files ignored due to filter (3)
  • apps/registry/package.json
  • pnpm-lock.yaml
  • turbo.json
Files selected for processing (6)
  • .github/workflows/ci.yml (2 hunks)
  • .github/workflows/playwright.yml (1 hunks)
  • apps/registry/.gitignore (1 hunks)
  • apps/registry/pages/api/[payload].js (2 hunks)
  • apps/registry/playwright.config.js (1 hunks)
  • apps/registry/tests/render.spec.js (1 hunks)
Files skipped from review due to trivial changes (2)
  • .github/workflows/playwright.yml
  • apps/registry/.gitignore
Additional comments: 13
.github/workflows/ci.yml (3)
  • 23-28:
    The lint job setup with Node.js version 18 and pnpm package manager is correctly configured.

  • 38-48:
    The build job setup with Node.js version 18, pnpm package manager, and Ruby with bundler cache is correctly configured.

  • 50-75:
    The test job setup with Node.js version 18, pnpm package manager, and Playwright browsers installation is correctly configured. However, ensure that the LEVINS_TOKEN used in the environment variable GITHUB_TOKEN is intended for this purpose and has the necessary permissions.

apps/registry/pages/api/[payload].js (2)
  • 15-19:
    The addition of the environment variable check for GITHUB_TOKEN is a good security practice and the error message provided is clear for developers.

  • 81-87:
    The conditional inclusion of the Authorization header in the HTTP request is a secure and flexible approach that allows for development without the token.

apps/registry/playwright.config.js (7)
  • 4-8:
    Verify whether the dotenv package is intentionally commented out and ensure that environment variables are being loaded correctly in all required environments.

  • 17-17:
    The reuseExistingServer configuration is correctly set to depend on the 'CI' environment variable, which is a good practice for CI environments.

  • 21-21:
    Ensure that test cases are designed to run in parallel without causing race conditions or shared state issues.

  • 25-25:
    The retry configuration is set appropriately for CI environments to help mitigate test flakiness.

  • 27-27:
    The worker configuration is set to 1 on CI to ensure reliable test execution, which is a good practice.

  • 33-33:
    Verify whether the baseURL is intentionally commented out and ensure that the correct base URL is being used in all required environments.

  • 39-75:
    The configuration for Chromium, Firefox, and WebKit browsers is set up correctly for cross-browser testing. The commented-out configurations for mobile and branded browsers are good placeholders for future test scenarios.

apps/registry/tests/render.spec.js (1)
  • 4-9:

The previous comments regarding the hardcoded URL and the non-descriptive test name are still valid and applicable.

Copy link
Collaborator Author

@levino levino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will go ahead and merge this in order to be able to merge #22. Please still read my comments @thomasdavis

@@ -14,6 +14,10 @@ const { Client } = require('pg');

const GITHUB_TOKEN = process.env.GITHUB_TOKEN;

if (!GITHUB_TOKEN && process.env.NODE_ENV !== 'development') {
throw new Error('GITHUB_TOKEN is not set in environment variables');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I did not specify the GITHUB_TOKEN in the environment, I would get an error message, that the github username does not exist, which made it harder to debug. This route will get issues, if we do not set a GITHUB_TOKEN because, as far as I understand, we will hit rate limits with the github api. This is why I think it is a good idea to fail hard and gracefully here instead of failing weirdly later

@@ -77,7 +81,7 @@ export default async function handler(req, res) {
`https://api.github.com/users/${username}/gists?per_page=100`,
{
headers: {
Authorization: 'Bearer ' + GITHUB_TOKEN,
...(GITHUB_TOKEN ? { Authorization: 'Bearer ' + GITHUB_TOKEN } : {}), // If we have no token and are in development, we can still make some requests.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a somewhat complicated but I find neat way to not set any headers when we have no GITHUB_TOKEN. The request will go through with any headers but will not with Authorization: NULL header. For development purposes (see also check above) it works without the GITHUB_TOKEN just fine. If someone hits rate limits during development, they can still set the token if they want.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b9c862d and d511f0f.
Files ignored due to filter (3)
  • apps/registry/package.json
  • pnpm-lock.yaml
  • turbo.json
Files selected for processing (5)
  • .github/workflows/ci.yml (2 hunks)
  • apps/registry/.gitignore (1 hunks)
  • apps/registry/pages/api/[payload].js (2 hunks)
  • apps/registry/playwright.config.js (1 hunks)
  • apps/registry/tests/render.spec.js (1 hunks)
Files skipped from review due to trivial changes (1)
  • apps/registry/.gitignore
Additional comments: 12
.github/workflows/ci.yml (3)
  • 23-28:
    The lint job setup with Node.js and pnpm is correctly configured.

  • 38-48:
    The build job setup with Node.js, pnpm, and Ruby is correctly configured.

  • 50-74:
    The test job setup with Playwright is correctly configured, including the installation of browsers and running of tests.

apps/registry/pages/api/[payload].js (2)
  • 15-19:
    The addition of the environment variable check for GITHUB_TOKEN is a good practice to prevent obscure errors and enforce proper configuration in non-development environments.

  • 81-87:
    The implementation of the conditional Authorization header based on the GITHUB_TOKEN allows for easier development without the token and is a good practice for maintaining flexibility.

apps/registry/playwright.config.js (7)
  • 4-8:
    Verify whether the environment variables required for the Playwright configuration are being set correctly in all environments, given that dotenv is commented out. If dotenv is necessary for local development or other non-CI environments, consider uncommenting it or providing clear instructions on how to set the required environment variables.

  • 17-17:
    The use of reuseExistingServer is correctly configured to improve efficiency during local development by reusing the server when not running in a CI environment.

  • 23-23:
    The forbidOnly configuration is correctly set to prevent accidentally committing .only in test cases, which could lead to incomplete test runs in CI.

  • 25-25:
    The retries configuration is set appropriately to handle potential flakiness in CI environments by allowing tests to be retried up to 2 times.

  • 29-29:
    Ensure that the CI system can handle and display HTML reports, or that there is a process in place to review these reports after test runs, as the reporter is set to 'html'.

  • 35-36:
    The trace configuration is set to 'on-first-retry', which is a good practice for debugging flaky tests by providing a trace of the first retry attempt.

  • 40-54:
    The configuration of projects for Chromium, Firefox, and WebKit browsers is a good practice for ensuring cross-browser compatibility of the application.

Comment on lines +4 to +9
test('has title', async ({ page }) => {
await page.goto('http://localhost:3002/thomasdavis');

// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(/Thomas Davis/);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using environment variables or a configuration file for the base URL (http://localhost:3002) to ensure the test is environment-agnostic. Additionally, the test name "has title" could be more descriptive, such as "Page title contains 'Thomas Davis'". This would provide more context when looking at test results. It would also be beneficial to include comments or documentation explaining the purpose of the test or the significance of the URL for maintainability.

/* Retry on CI only */
retries: process.env['CI'] ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env['CI'] ? 1 : undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider the impact of setting workers to 1 in CI environments, as this will cause tests to run sequentially and could lead to longer test execution times. If parallel execution is stable in the CI environment, allowing more workers could speed up the test suite.

@levino levino added this pull request to the merge queue Nov 26, 2023
@levino levino removed this pull request from the merge queue due to a manual request Nov 26, 2023
@levino levino added this pull request to the merge queue Nov 26, 2023
@levino levino removed this pull request from the merge queue due to a manual request Nov 26, 2023
@levino levino added this pull request to the merge queue Nov 26, 2023
Merged via the queue into master with commit ebb20bf Nov 26, 2023
4 checks passed
@levino levino deleted the feature/e2e-tests branch November 26, 2023 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant