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

chore: backend e2e tests CI #34

Merged
merged 2 commits into from
Oct 20, 2024
Merged

chore: backend e2e tests CI #34

merged 2 commits into from
Oct 20, 2024

Conversation

krystxf
Copy link
Owner

@krystxf krystxf commented Oct 20, 2024

Summary by CodeRabbit

  • New Features

    • Updated PostgreSQL user and password environment variables in configuration files.
    • Introduced a dedicated end-to-end testing job in the CI workflow.
  • Bug Fixes

    • Optimized test setup and teardown processes for various modules by changing lifecycle hooks.
  • Chores

    • Minor formatting updates to environment variable files.

@krystxf krystxf added the CI label Oct 20, 2024
@krystxf krystxf self-assigned this Oct 20, 2024
Copy link

vercel bot commented Oct 20, 2024

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

Name Status Preview Comments Updated (UTC)
metro-now ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 20, 2024 0:58am

Copy link

coderabbitai bot commented Oct 20, 2024

Walkthrough

This pull request includes updates to environment variable configurations in two example files, .env.docker.example and .env.local.example, specifically changing the PostgreSQL user and password values. Additionally, it modifies the GitHub Actions workflow for backend continuous integration by renaming the existing CI job and adding a new end-to-end testing job. Furthermore, it optimizes the lifecycle hooks in several end-to-end test files, changing beforeEach and afterEach to beforeAll and afterAll, respectively, to improve test execution efficiency.

Changes

File Path Change Summary
.env.docker.example Updated POSTGRES_USER from pg_user to postgres_user, and POSTGRES_PASSWORD from pg_password to postgres_password.
apps/backend/.env.local.example Updated POSTGRES_USER from pg_user to postgres_user, and POSTGRES_PASSWORD from pg_password to postgres_password. Added a newline after REDIS_PORT.
.github/workflows/backend-ci.yaml Renamed CI job from "Backend CI" to "Backend". Added new job e2e for end-to-end testing. Modified existing CI job steps, including renaming "Build" to "Prisma generate" and adding a new "Build" step.
apps/backend/e2e/tests/departure.e2e-spec.ts Changed lifecycle hooks from beforeEach to beforeAll and afterEach to afterAll.
apps/backend/e2e/tests/platform.e2e-spec.ts Changed lifecycle hooks from beforeEach to beforeAll and afterEach to afterAll.
apps/backend/e2e/tests/stop.e2e-spec.ts Changed lifecycle hooks from beforeEach to beforeAll and afterEach to afterAll.

Possibly related PRs

  • chore(be): E2E testing #30: The changes in this PR involve updates to environment variables in the .env.local.example file, which are directly related to the modifications made in the main PR to the .env.docker.example file, specifically the updates to POSTGRES_USER and POSTGRES_PASSWORD.

Suggested labels

backend, dependencies


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@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.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (10)
apps/backend/.env.local.example (1)

5-6: Approved: Improved clarity in example credentials.

The changes to POSTGRES_USER and POSTGRES_PASSWORD improve clarity and consistency with the .env.docker.example file. This is a good practice for maintaining uniform example configurations across different environment setups.

Consider adding a comment above these lines to explicitly state that these are example values and should be changed in actual deployments. For example:

# postgres
+# Note: Replace these example values with secure credentials in actual deployments
POSTGRES_USER=postgres_user
POSTGRES_PASSWORD=postgres_password
.env.docker.example (1)

5-6: Security consideration: Descriptive credential names in example files

While using more descriptive names for credentials can improve clarity, it's worth noting that in example files, overly descriptive names might provide unnecessary information to potential attackers. Consider using generic placeholders instead, such as YOUR_POSTGRES_USER and YOUR_POSTGRES_PASSWORD.

Here's a suggested change:

-POSTGRES_USER=postgres_user
-POSTGRES_PASSWORD=postgres_password
+POSTGRES_USER=YOUR_POSTGRES_USER
+POSTGRES_PASSWORD=YOUR_POSTGRES_PASSWORD

This approach maintains clarity while avoiding potential security concerns in example files.

apps/backend/e2e/tests/departure.e2e-spec.ts (1)

30-32: Approve change from afterEach to afterAll with a suggestion

This change aligns well with the beforeAll modification and can improve test suite performance by closing the NestJS application only once after all tests are complete.

For added robustness, consider wrapping the app.close() call in a try-catch block to ensure that any errors during teardown don't prevent other cleanup operations or cause the test suite to hang.

Here's a suggested improvement:

 afterAll(async () => {
-  await app.close();
+  try {
+    await app.close();
+  } catch (error) {
+    console.error('Error during app closure:', error);
+  }
 });
apps/backend/e2e/tests/platform.e2e-spec.ts (3)

Line range hint 19-32: Approved: Lifecycle hook optimization

The change from beforeEach to beforeAll is a good optimization that can significantly reduce the overall test execution time by initializing the application only once for all tests in this suite.

However, be aware that this change might introduce shared state between tests. Ensure that:

  1. Individual tests don't modify global state that could affect other tests.
  2. If database operations are performed, consider implementing a cleanup mechanism or use transactions to isolate test data.

34-36: Approved: Consistent lifecycle hook change

The change from afterEach to afterAll is consistent with the earlier beforeAll modification. This ensures that the application is closed only once after all tests in the suite have completed, which is more efficient.

To further improve the robustness of the cleanup process, consider adding a try-catch block:

afterAll(async () => {
  try {
    await app.close();
  } catch (error) {
    console.error('Error during app closure:', error);
  }
});

This will ensure that any errors during the closure process are logged and don't cause the entire test suite to fail unexpectedly.


Line range hint 19-36: Summary: Positive impact on test suite performance

The changes to the lifecycle hooks (beforeAll and afterAll) should significantly improve the performance of this test suite by reducing the number of times the application is initialized and torn down. This aligns well with the PR objective of improving CI for backend e2e tests.

To further enhance the test suite:

  1. Consider adding a test for application startup and shutdown to ensure these processes work correctly, as they now happen only once per suite.
  2. If not already implemented, consider adding parallel test execution capabilities to take full advantage of the optimized setup.
  3. Monitor the execution time of this test suite in CI to quantify the performance improvement.

Would you like assistance in implementing any of these suggestions?

.github/workflows/backend-ci.yaml (4)

1-1: Consider keeping "CI" in the workflow name for clarity.

The workflow name has been changed from "Backend CI" to "Backend". While this is more concise, it might be less specific. Consider using "Backend CI/CD" or "Backend Pipeline" to maintain clarity about the workflow's purpose.


30-38: Approve changes with minor formatting suggestion.

The separation of Prisma generation and build steps improves clarity and maintainability. This change is beneficial.

Remove trailing spaces on line 37 to adhere to YAML best practices:

-                  cd apps/backend 
+                  cd apps/backend
🧰 Tools
🪛 yamllint

[error] 37-37: trailing spaces

(trailing-spaces)


83-125: Approve setup steps with minor suggestions.

The setup steps for the e2e job are comprehensive and well-structured, following CI best practices. The environment setup, Prisma generation, migrations, and seeding are all appropriately included.

Consider the following minor improvements:

  1. On line 106, use printf instead of echo to ensure proper expansion of escape sequences:
-                  echo "\n" >> ".env.local"
+                  printf "\n" >> ".env.local"
  1. Remove trailing spaces on lines 119 and 124:
-                  cd apps/backend 
+                  cd apps/backend
-                  cd apps/backend  
+                  cd apps/backend
🧰 Tools
🪛 actionlint

102-102: shellcheck reported issue in this script: SC2028:info:3:6: echo may not expand escape sequences. Use printf

(shellcheck)

🪛 yamllint

[error] 119-119: trailing spaces

(trailing-spaces)


[error] 124-124: trailing spaces

(trailing-spaces)


127-130: LGTM: E2E test execution step.

The e2e test execution step is appropriately configured using pnpm test:e2e.

Remove the trailing space on line 129:

-                  cd apps/backend 
+                  cd apps/backend
🧰 Tools
🪛 yamllint

[error] 129-129: trailing spaces

(trailing-spaces)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 46c0cfe and 75235d2.

📒 Files selected for processing (6)
  • .env.docker.example (1 hunks)
  • .github/workflows/backend-ci.yaml (3 hunks)
  • apps/backend/.env.local.example (2 hunks)
  • apps/backend/e2e/tests/departure.e2e-spec.ts (2 hunks)
  • apps/backend/e2e/tests/platform.e2e-spec.ts (2 hunks)
  • apps/backend/e2e/tests/stop.e2e-spec.ts (2 hunks)
🧰 Additional context used
🪛 yamllint
.github/workflows/backend-ci.yaml

[error] 37-37: trailing spaces

(trailing-spaces)


[error] 119-119: trailing spaces

(trailing-spaces)


[error] 124-124: trailing spaces

(trailing-spaces)


[error] 129-129: trailing spaces

(trailing-spaces)

🪛 actionlint
.github/workflows/backend-ci.yaml

102-102: shellcheck reported issue in this script: SC2028:info:3:6: echo may not expand escape sequences. Use printf

(shellcheck)

🔇 Additional comments (10)
apps/backend/.env.local.example (1)

26-26: LGTM: Improved formatting.

The added newline after REDIS_PORT improves readability and maintains consistency with the formatting of other sections in the file.

.env.docker.example (1)

5-6: Credential updates: Ensure consistency across the project

The changes to POSTGRES_USER and POSTGRES_PASSWORD appear to be part of a standardization effort. While this improves clarity, please ensure the following:

  1. Update all related configurations, scripts, and services that might be using these credentials to prevent any connection issues.
  2. Verify that these changes are consistent with the updates made in apps/backend/.env.local.example as mentioned in the summary.
  3. Update any project documentation that references these environment variables.

To verify the consistency of these changes across the project, run the following script:

This will help identify any places where the old or new credential names are used, ensuring complete updates across the project.

✅ Verification successful

Credential updates verified successfully

The changes to POSTGRES_USER and POSTGRES_PASSWORD have been consistently updated across the project. No instances of the old credential names (pg_user, pg_password) were found, and the new names are used uniformly in the relevant configuration files.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for occurrences of old and new credential names

echo "Checking for old credential names:"
rg 'pg_user|pg_password' --type-not sql

echo "Checking for new credential names:"
rg 'postgres_user|postgres_password' --type-not sql

Length of output: 485

apps/backend/e2e/tests/stop.e2e-spec.ts (3)

30-30: Approve change to afterAll.

The change from afterEach to afterAll is consistent with the earlier beforeAll modification. This further optimizes the test suite by reducing teardown overhead.

This change is appropriate given the beforeAll setup, as it ensures the application is properly closed after all tests have run.


Line range hint 15-32: Optimize e2e test performance and suggest measurement.

The changes to lifecycle hooks (beforeAll and afterAll) should significantly improve the efficiency of this e2e test suite. This optimization aligns well with the PR objective of improving CI for backend e2e tests.

To quantify the improvement, consider measuring and comparing the execution time before and after these changes. You can use the following script in your CI pipeline:

#!/bin/bash
# Description: Measure and compare e2e test execution time

# Run the tests and capture the execution time
start_time=$(date +%s)
npm run test:e2e -- apps/backend/e2e/tests/stop.e2e-spec.ts
end_time=$(date +%s)

# Calculate and print the execution time
execution_time=$((end_time - start_time))
echo "E2E test execution time: $execution_time seconds"

# Compare with previous runs (you'll need to implement a mechanism to store and retrieve previous times)
# For example, you could store the time in a file and compare:
# previous_time=$(cat previous_time.txt)
# echo "Previous execution time: $previous_time seconds"
# echo "Difference: $((previous_time - execution_time)) seconds"

# Store the current time for future comparisons
echo $execution_time > previous_time.txt

This script will help you track performance improvements over time and ensure that the changes are having the desired effect on CI efficiency.


15-15: Approve change to beforeAll, but verify test independence.

The change from beforeEach to beforeAll can significantly improve test performance by reducing setup overhead. This is a good optimization for e2e tests.

However, please ensure that all tests in this suite are independent and don't rely on a clean state for each test case. If any tests modify the application state, they should clean up after themselves.

To verify test independence, you can run the following script:

This script will help identify any database operations or state modifications within individual test cases, which might indicate potential issues with the beforeAll approach.

apps/backend/e2e/tests/departure.e2e-spec.ts (2)

15-15: Approve change from beforeEach to beforeAll

This change improves the performance of the test suite by setting up the NestJS application only once for all tests, rather than before each individual test. This is particularly beneficial for end-to-end tests where application setup can be time-consuming.

Make sure that individual tests don't modify the application state in ways that could affect other tests. If they do, consider adding cleanup steps within the tests themselves.


Line range hint 15-32: Summary of changes and their impact

The modifications to this file, changing beforeEach to beforeAll and afterEach to afterAll, are part of a broader optimization effort for e2e tests. These changes can significantly improve test execution time by reducing the number of times the NestJS application is set up and torn down.

While this approach slightly reduces test isolation, it's generally an acceptable trade-off for e2e tests, especially when dealing with time-consuming operations like application setup.

To maintain test reliability:

  1. Ensure tests don't modify shared state in ways that affect other tests.
  2. If necessary, add test-specific setup or cleanup steps within individual test cases.
  3. Monitor the e2e test suite for any new flakiness or unexpected behaviors that might arise from these changes.

Overall, these changes should lead to faster and more efficient e2e testing without significantly compromising test quality.

.github/workflows/backend-ci.yaml (3)

55-57: LGTM: New e2e job added.

The addition of a dedicated e2e job aligns well with the PR objectives for implementing backend e2e tests in CI. Running on ubuntu-latest is a good choice for most CI/CD workflows.


58-81: LGTM: Well-configured services for e2e testing.

The PostgreSQL and Redis services are well-configured with appropriate health checks and standard port mappings. The PostgreSQL credentials (postgres_user/postgres_password) align with the changes mentioned in the .env files, ensuring consistency across the CI environment.


Line range hint 1-130: Overall: Well-implemented CI/CD workflow for backend e2e tests.

The changes in this file successfully implement the backend e2e tests in CI, aligning perfectly with the PR objectives. The workflow structure is solid, with appropriate job separation, service configurations, and comprehensive setup steps. The minor formatting issues identified (trailing spaces and echo usage) do not impact functionality but addressing them would improve code quality.

Great job on implementing this CI/CD workflow for backend e2e tests!

🧰 Tools
🪛 actionlint

102-102: shellcheck reported issue in this script: SC2028:info:3:6: echo may not expand escape sequences. Use printf

(shellcheck)

🪛 yamllint

[error] 119-119: trailing spaces

(trailing-spaces)


[error] 124-124: trailing spaces

(trailing-spaces)


[error] 129-129: trailing spaces

(trailing-spaces)

@krystxf krystxf merged commit 7bfb554 into main Oct 20, 2024
13 checks passed
@krystxf krystxf deleted the chore/e2e-ci branch October 20, 2024 13:04
This was referenced Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant