Skip to content

Commit

Permalink
Merge pull request #90 from hiteshchoudhary/feat/coverage
Browse files Browse the repository at this point in the history
feat: code coverage with playwright
  • Loading branch information
wajeshubham authored Mar 3, 2024
2 parents 17724d1 + deffc3c commit 79541d5
Show file tree
Hide file tree
Showing 21 changed files with 3,920 additions and 642 deletions.
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-syntax-import-assertions"]
}
3 changes: 2 additions & 1 deletion .commitlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"refactor",
"revert",
"style",
"assets"
"assets",
"test"
]
]
}
Expand Down
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ################# REQUIRED ENV VARS START #################
PORT=8080
MONGO_MEMORY_SERVER_PORT=10000 # mongodb port for e2e testing
MONGODB_URI=mongodb://mongodb:27017 # `mongodb://localhost:27017` in case using local mongodb
NODE_ENV=development # changing this will avoid stack traces in the error response
EXPRESS_SESSION_SECRET=7fdOMCFRSLD9cv1k-5n3Dz5n3DmVmVHVIg9GG_OGTUkBfLNdgZAwKDNtoCJ0X0cyqaM0ogR80-zh9kx0Mkx # ok to change
Expand Down
23 changes: 23 additions & 0 deletions .github/ISSUE_TEMPLATE/code_coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: 🧪 Testing Contribution
description: Share details about your test coverage for contribution to FreeAPI.
title: "TESTING: <title>"

labels:
- testing

body:
- type: textarea
id: test_description
attributes:
label: Describe the test
description: A clear and concise description of the proposed testing suite.
validations:
required: true

- type: textarea
id: additional_info
attributes:
label: Additional Information
description: Add any extra details or considerations that might be relevant such as screenshots.
validations:
required: false
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ logs

/tmp
/.vscode
/.idea

# ignore the image files uploaded in the public/images folder
/public/images/*
Expand All @@ -145,3 +146,9 @@ logs

NOTES.md
.DS_Store

# Playwright
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
9 changes: 5 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ To contribute to FreeAPI, please follow these guidelines:

1. Fork the repository on GitHub.
2. Clone your forked repository to your local machine.
3. Create a new branch for your feature or bug fix: `git checkout -b feat/your-feature-name` or `git checkout -b fix/your-bug-fix-name`.
4. Make your changes and commit them with descriptive messages: `git commit -am 'add your commit message'`.
5. Push your changes to your forked repository: `git push origin feat/your-feature-name`.
6. Submit a pull request to the main repository.
3. Create a new branch for your feature or bug fix: `git checkout -b feat/your-feature-name` or `git checkout -b fix/your-bug-fix-name` and make your changes.
4. Run all the tests 🧪 before committing the changes and make sure all tests are passed.
5. After all tests are passed, commit your changes with a descriptive messages: `git commit -am 'add your commit message'`.
6. Push your changes to your forked repository: `git push origin feat/your-feature-name`.
7. Submit a pull request to the main repository.

## Commit Message Format <a name="commit-message-format"></a>

Expand Down
110 changes: 110 additions & 0 deletions CONTRIBUTING_CODE_COVERAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# FreeAPI Testing Contribution Guide

Thank you for your interest in contributing to the FreeAPI project to help us deliver our APIs that are battlefield tested. To ensure reliability & stability for our end users, we utilize Playwright, a powerful testing framework to automate testing across all endpoints.

### Why Playwright?

After exploring our open-source community options such as Jest, Jasmine & Playwright. We decided to move forward with the playwright as it facilitates automated testing, offers cross-browser support, rich enough API with familiar syntax.

## ⚠️ Important Note:

### Before starting your contribution

- create an issue stating which module you will be working on
- in a given module if tests exist, we do not welcome them as long as it is a logical fix
- test coverage for example frontend apps are not our top priority

**IMPORTANT: Contributor must create an issue with [Testing Contribution](https://github.com/hiteshchoudhary/apihub/issues/new?assignees=&labels=testing&projects=&template=code_coverage.yaml&title=TESTING%3A+%3Ctitle%3E) issue template.**

This ensures coordination and prevents duplicated efforts.

## Table of Contents

1. [🏁 Getting Started](#getting-started)
2. [👆🏻 Choosing a Module](#choosing-a-module)
3. [🗂️ Folder Structure](#folder-structure-main)
4. [📙 Coding Standards](#coding-standards)
5. [📝 Dependency Management](#dependency-management)
6. [📨 Submitting Your Contribution](#submitting-your-contribution)

## Getting Started <a name="getting-started"></a>

### Fork the Repository

Start by forking the FreeAPI project repository to your GitHub account.

### Clone Your Fork

Clone your fork of the repository to your local machine.

```bash
git clone https://github.com/<your_username>/apihub.git
cd apihub
```

### Install Dependencies

Make sure you have the necessary dependencies of FreeAPI installed for the frontend framework or library you plan to use.

Follow this [README.md section](https://github.com/hiteshchoudhary/apihub/blob/main/README.md#-installation) to know more about setting up the FreeAPI environment

## Choosing a Module <a name="choosing-a-module"></a>

Decide which module you want to contribute to. Browse the `/apps`, `/public`, or `/kitchen-sink` modules to explore the available modules and APIs. Read the following section carefully to understand the folder structure you need to follow to increase your chances of getting your PR approved.

## Folder Structure <a name="folder-structure-main"></a>

Follow the specified folder structure for your frontend application (**The folder names must not follow the camel casing to keep things consistent.** _Your actual project code folders may have camel casing_):

```
ROOT_FOLDER/e2e/{module}/
```

See the following examples with context for the above structure:

## Example: Todo endpoint testing

Imagine you want to test the todo endpoint that is part of `/apps` module of the FreeAPI project. To keep a consistent folder structure for backtracking name your files with name identifier, but with an extension of `.test.js`.

### Folder Structure:

```
$ROOT_FOLDER/e2e/{package}/{module}/{file-indicator}.test.js
```

## Explanation for the examples:

- `ROOT_FOLDER`: Refers to the root directory of the FreeAPI project.

- `e2e`: This directory is designated for including test cases.

- `{package}`: The directory contains typical of a Node.js application: `app.js` initializes the app, `controllers` handle requests, `models` define data structures, `routes` map endpoints, `utils` provide utilities, and others manage logging, authentication, and database interactions.

- `{module}`: `apps` or `public` or `kitchen-sink`: Denotes the chosen module (`/apps` for complex apps, `/public` for public APIs, `/kitchen-sink` for backend-related static APIs. These folders are already created officially).

- `{file-indicator}`: This is the actual file name indicator that helps to identify for which file you are writing test cases for example: `todo.test.js`

By following this standardized folder structure, contributors can easily organize their front-end projects, making it convenient for others to explore, understand, and replicate the implementation.

## Coding Standards <a name="coding-standards"></a>

Adhere to the coding standards of the playwright framework. Additionally, consider the following guidelines:

- Use clear descriptive test suite & block names
- Follow best practices for unit, integration & end-to-end testing
- Ensure your code is well-documented by comments wherever necessary
- Make sure to add both positive & negative test cases

## Dependency Management <a name="dependency-management"></a>

Ideally, we do not encourage you to include a new package. Confine your code practices within the available dependencies to avoid overheads. Please state with a clear explanation with examples if you add anything new.

## Submitting Your Contribution <a name="submitting-your-contribution"></a>

Click [here](https://github.com/hiteshchoudhary/apihub/blob/main/CONTRIBUTING.md) for a detailed contribution guide on submitting a PR.

Thank you for your contribution to FreeAPI! Your dedication helps make our APIs more accessible and valuable to the community. If you have any questions or need assistance, feel free to reach out to our [Discord](https://discord.gg/uuGBkhX8Yp).

Thank you for being part of the FreeAPI community and contributing to the project! We look forward to featuring your fantastic work. 🌟

Happy coding! 🚀
110 changes: 72 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,44 +77,6 @@ This ensures that your work is not affected by the server resets and allows you
To self-host the FreeAPI.app application, you can take advantage of a pre-built template that is readily available.
[Click here for detailed docs](https://github.com/hiteshchoudhary/apihub/#-using-railway-one-click-deploy)

# How to contribute - Guidelines

## Contribute in core FreeAPI codebase:

We welcome your interest in contributing to our open source project!

To contribute to FreeAPI, please follow these steps:

1. Fork the repository.
2. Create a new branch for your feature or bug fix: `git checkout -b feat/your-feature-name` or `git checkout -b fix/your-bug-fix-name`.
3. Make your changes and commit them using conventional commit messages: `git commit -am "feat: Add new feature"`. For more details on the commit format and other guidelines, please refer to the [Contributor Guidelines](./CONTRIBUTING.md).
4. Push your changes to your forked repository: `git push origin feat/your-feature-name`.
5. Submit a pull request to the main repository, explaining the changes you've made and providing any necessary details.

Here's a guide on how you can effectively contribute to our API hub:

1. Pull Requests for Readme Updates: Please refrain from sending pull requests solely for updating the project's readme file. While we appreciate the importance of clear and concise documentation, we prefer to focus on substantial code contributions and feature enhancements.

2. Grammar Updates: Our team values effective communication, but we're not grammar sticklers. You don't need to send pull requests solely for grammar fixes or minor language improvements. Instead, concentrate on the core functionalities and features of the project.

3. Avoid Updating Existing Public APIs: To maintain stability and consistency, we discourage direct updates to existing public APIs within the API hub. These APIs have been thoroughly tested and approved. However, if you encounter any bugs or issues, we encourage you to open an issue on our project's issue tracker to notify us.

4. Build New Project APIs: We encourage you to explore your creativity and contribute by building complete project APIs. These APIs should provide comprehensive solutions that can assist developers in constructing complex projects to showcase their skills and abilities. Your contributions in this area will greatly benefit the community.

5. Draft a Proposal and Discuss on Discord: Before diving into your project, we recommend drafting a proposal. This can include a mind map or outline of the API you intend to build and its potential benefits. Join our Discord community, where you can share your proposal, discuss ideas, and gather feedback from fellow contributors. Engaging in these discussions will enhance your backend portfolio and help shape the future direction of the project.

We appreciate your enthusiasm and look forward to your valuable contributions to our open source API hub project.

Together, we can foster a collaborative environment and make a significant impact in the API integration landscape.

Click [here](https://github.com/hiteshchoudhary/apihub/blob/main/CONTRIBUTING.md) for detailed contribution guide.

## Contribute by creating frontend application:

Thank you for your interest in contributing to the FreeAPI project by creating frontend applications consuming FreeAPIs! Your efforts play a crucial role in enhancing the user experience and expanding the reach of our APIs. Please follow this guide to ensure a smooth and collaborative contribution process.

Click [here](https://github.com/hiteshchoudhary/apihub/blob/main/CONTRIBUTING_FRONTEND.md) for detailed contribution guide for Frontend Developers 🚀!

# 🏁 Installation

### 📦 Using Docker (recommended)
Expand Down Expand Up @@ -193,6 +155,78 @@ To self-host the FreeAPI.app application, you can take advantage of a pre-built

Note: Once the application is deployed, please wait for 3-5 minutes for the swagger docs to be available.

# 🧪 Testing

To ensure reliability & stability for our end users, we utilize Playwright, a powerful testing framework to automate testing across all endpoints.

### 💻 Run the Test Server

Make sure to add `MONGO_MEMORY_SERVER_PORT=10000` (mongodb port for e2e testing) in your `.env` file.

```bash
yarn start:test-server
```

### 🧪 Run Tests

```bash
yarn test:playwright
```

This will generate a Playwright report. To view this report run the following command

```bash
yarn playwright show-report
```

Make sure all the test cases are passed whenever you make any changes.

# How to contribute - Guidelines

## ⚡️ Contribute in core FreeAPI codebase:

We welcome your interest in contributing to our open source project!

To contribute to FreeAPI, please follow these steps:

1. Fork the repository.
2. Create a new branch for your feature or bug fix: `git checkout -b feat/your-feature-name` or `git checkout -b fix/your-bug-fix-name` and make your changes.
3. Run all the tests 🧪 before committing the changes and make sure all tests are passed.
4. After all tests are passed, commit your changes with a descriptive messages: `git commit -am 'add your commit message'`
5. For more details on the commit format and other guidelines, please refer to the [Contributor Guidelines](./CONTRIBUTING.md).
6. Push your changes to your forked repository: `git push origin feat/your-feature-name`.
7. Submit a pull request to the main repository, explaining the changes you've made and providing any necessary details.

Here's a guide on how you can effectively contribute to our API hub:

1. Pull Requests for Readme Updates: Please refrain from sending pull requests solely for updating the project's readme file. While we appreciate the importance of clear and concise documentation, we prefer to focus on substantial code contributions and feature enhancements.

2. Grammar Updates: Our team values effective communication, but we're not grammar sticklers. You don't need to send pull requests solely for grammar fixes or minor language improvements. Instead, concentrate on the core functionalities and features of the project.

3. Avoid Updating Existing Public APIs: To maintain stability and consistency, we discourage direct updates to existing public APIs within the API hub. These APIs have been thoroughly tested and approved. However, if you encounter any bugs or issues, we encourage you to open an issue on our project's issue tracker to notify us.

4. Build New Project APIs: We encourage you to explore your creativity and contribute by building complete project APIs. These APIs should provide comprehensive solutions that can assist developers in constructing complex projects to showcase their skills and abilities. Your contributions in this area will greatly benefit the community.

5. Draft a Proposal and Discuss on Discord: Before diving into your project, we recommend drafting a proposal. This can include a mind map or outline of the API you intend to build and its potential benefits. Join our Discord community, where you can share your proposal, discuss ideas, and gather feedback from fellow contributors. Engaging in these discussions will enhance your backend portfolio and help shape the future direction of the project.

We appreciate your enthusiasm and look forward to your valuable contributions to our open source API hub project.

Together, we can foster a collaborative environment and make a significant impact in the API integration landscape.

Click [here](https://github.com/hiteshchoudhary/apihub/blob/main/CONTRIBUTING.md) for detailed contribution guide.

## 🚀 Contribute by creating frontend application:

Thank you for your interest in contributing to the FreeAPI project by creating frontend applications consuming FreeAPIs! Your efforts play a crucial role in enhancing the user experience and expanding the reach of our APIs. Please follow this guide to ensure a smooth and collaborative contribution process.

Click [here](https://github.com/hiteshchoudhary/apihub/blob/main/CONTRIBUTING_FRONTEND.md) for detailed contribution guide for Frontend Developers 🚀!

## 🧪 Contribute in testing suite

Thank you for your interest in contributing to the FreeAPI project to increase code coverage of our API service that helps us to ship robust endpoints that are battlefield tested. Please follow this guide to ensure a smooth and collaborative contribution process.

Click [here](https://github.com/hiteshchoudhary/apihub/blob/main/CONTRIBUTING_CODE_COVERAGE.md) for detailed contribution guide for increasing code coverage.

# 📜 Swagger Docs

[Swagger Docs](https://api.freeapi.app): https://api.freeapi.app
12 changes: 12 additions & 0 deletions e2e/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import dotenv from "dotenv";

dotenv.config({
path: "../.env",
});

export const URL = `http://localhost:${process.env.PORT || 8080}`;

export const getApiContext = async (playwright) =>
playwright.request.newContext({
baseURL: URL,
});
40 changes: 40 additions & 0 deletions e2e/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import mongoose, { mongo } from "mongoose";
import { MongoMemoryServer } from "mongodb-memory-server";

const MONGO_MEMORY_SERVER_PORT = process.env.MONGO_MEMORY_SERVER_PORT || 10000;
const MONGODB_URL = `mongodb://127.0.0.1:${MONGO_MEMORY_SERVER_PORT}/`;

let mongoServer = null;
let dbInstance = undefined;

const connectDB = async () => {
try {
await mongoose.disconnect();
mongoServer = await MongoMemoryServer.create({
instance: {
port: +MONGO_MEMORY_SERVER_PORT,
},
});
dbInstance = await mongoose.connect(MONGODB_URL);
} catch (error) {
console.error("Mongo db connect error: ", error);
process.exit(1);
}
};
export const clearDB = async (collectionName = null) => {
if (!dbInstance) {
dbInstance = await mongoose.connect(MONGODB_URL);
}
const connection = mongoose.connection;
if (collectionName) {
await connection.db.collection(collectionName).deleteMany({});
} else {
const collections = await connection.db.listCollections().toArray();
const collectionNames = collections.map((col) => col.name);
for (let name of collectionNames) {
await connection.db.collection(name).deleteMany({});
}
}
};

export default connectDB;
Loading

0 comments on commit 79541d5

Please sign in to comment.