Skip to content

Commit 79541d5

Browse files
authored
Merge pull request #90 from hiteshchoudhary/feat/coverage
feat: code coverage with playwright
2 parents 17724d1 + deffc3c commit 79541d5

21 files changed

+3920
-642
lines changed

.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["@babel/preset-env"],
3+
"plugins": ["@babel/plugin-syntax-import-assertions"]
4+
}

.commitlintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"refactor",
1515
"revert",
1616
"style",
17-
"assets"
17+
"assets",
18+
"test"
1819
]
1920
]
2021
}

.env.sample

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# ################# REQUIRED ENV VARS START #################
22
PORT=8080
3+
MONGO_MEMORY_SERVER_PORT=10000 # mongodb port for e2e testing
34
MONGODB_URI=mongodb://mongodb:27017 # `mongodb://localhost:27017` in case using local mongodb
45
NODE_ENV=development # changing this will avoid stack traces in the error response
56
EXPRESS_SESSION_SECRET=7fdOMCFRSLD9cv1k-5n3Dz5n3DmVmVHVIg9GG_OGTUkBfLNdgZAwKDNtoCJ0X0cyqaM0ogR80-zh9kx0Mkx # ok to change
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: 🧪 Testing Contribution
2+
description: Share details about your test coverage for contribution to FreeAPI.
3+
title: "TESTING: <title>"
4+
5+
labels:
6+
- testing
7+
8+
body:
9+
- type: textarea
10+
id: test_description
11+
attributes:
12+
label: Describe the test
13+
description: A clear and concise description of the proposed testing suite.
14+
validations:
15+
required: true
16+
17+
- type: textarea
18+
id: additional_info
19+
attributes:
20+
label: Additional Information
21+
description: Add any extra details or considerations that might be relevant such as screenshots.
22+
validations:
23+
required: false

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ logs
134134

135135
/tmp
136136
/.vscode
137+
/.idea
137138

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

146147
NOTES.md
147148
.DS_Store
149+
150+
# Playwright
151+
/test-results/
152+
/playwright-report/
153+
/blob-report/
154+
/playwright/.cache/

CONTRIBUTING.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ To contribute to FreeAPI, please follow these guidelines:
1717

1818
1. Fork the repository on GitHub.
1919
2. Clone your forked repository to your local machine.
20-
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`.
21-
4. Make your changes and commit them with descriptive messages: `git commit -am 'add your commit message'`.
22-
5. Push your changes to your forked repository: `git push origin feat/your-feature-name`.
23-
6. Submit a pull request to the main repository.
20+
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.
21+
4. Run all the tests 🧪 before committing the changes and make sure all tests are passed.
22+
5. After all tests are passed, commit your changes with a descriptive messages: `git commit -am 'add your commit message'`.
23+
6. Push your changes to your forked repository: `git push origin feat/your-feature-name`.
24+
7. Submit a pull request to the main repository.
2425

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

CONTRIBUTING_CODE_COVERAGE.md

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# FreeAPI Testing Contribution Guide
2+
3+
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.
4+
5+
### Why Playwright?
6+
7+
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.
8+
9+
## ⚠️ Important Note:
10+
11+
### Before starting your contribution
12+
13+
- create an issue stating which module you will be working on
14+
- in a given module if tests exist, we do not welcome them as long as it is a logical fix
15+
- test coverage for example frontend apps are not our top priority
16+
17+
**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.**
18+
19+
This ensures coordination and prevents duplicated efforts.
20+
21+
## Table of Contents
22+
23+
1. [🏁 Getting Started](#getting-started)
24+
2. [👆🏻 Choosing a Module](#choosing-a-module)
25+
3. [🗂️ Folder Structure](#folder-structure-main)
26+
4. [📙 Coding Standards](#coding-standards)
27+
5. [📝 Dependency Management](#dependency-management)
28+
6. [📨 Submitting Your Contribution](#submitting-your-contribution)
29+
30+
## Getting Started <a name="getting-started"></a>
31+
32+
### Fork the Repository
33+
34+
Start by forking the FreeAPI project repository to your GitHub account.
35+
36+
### Clone Your Fork
37+
38+
Clone your fork of the repository to your local machine.
39+
40+
```bash
41+
git clone https://github.com/<your_username>/apihub.git
42+
cd apihub
43+
```
44+
45+
### Install Dependencies
46+
47+
Make sure you have the necessary dependencies of FreeAPI installed for the frontend framework or library you plan to use.
48+
49+
Follow this [README.md section](https://github.com/hiteshchoudhary/apihub/blob/main/README.md#-installation) to know more about setting up the FreeAPI environment
50+
51+
## Choosing a Module <a name="choosing-a-module"></a>
52+
53+
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.
54+
55+
## Folder Structure <a name="folder-structure-main"></a>
56+
57+
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_):
58+
59+
```
60+
ROOT_FOLDER/e2e/{module}/
61+
```
62+
63+
See the following examples with context for the above structure:
64+
65+
## Example: Todo endpoint testing
66+
67+
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`.
68+
69+
### Folder Structure:
70+
71+
```
72+
$ROOT_FOLDER/e2e/{package}/{module}/{file-indicator}.test.js
73+
```
74+
75+
## Explanation for the examples:
76+
77+
- `ROOT_FOLDER`: Refers to the root directory of the FreeAPI project.
78+
79+
- `e2e`: This directory is designated for including test cases.
80+
81+
- `{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.
82+
83+
- `{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).
84+
85+
- `{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`
86+
87+
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.
88+
89+
## Coding Standards <a name="coding-standards"></a>
90+
91+
Adhere to the coding standards of the playwright framework. Additionally, consider the following guidelines:
92+
93+
- Use clear descriptive test suite & block names
94+
- Follow best practices for unit, integration & end-to-end testing
95+
- Ensure your code is well-documented by comments wherever necessary
96+
- Make sure to add both positive & negative test cases
97+
98+
## Dependency Management <a name="dependency-management"></a>
99+
100+
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.
101+
102+
## Submitting Your Contribution <a name="submitting-your-contribution"></a>
103+
104+
Click [here](https://github.com/hiteshchoudhary/apihub/blob/main/CONTRIBUTING.md) for a detailed contribution guide on submitting a PR.
105+
106+
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).
107+
108+
Thank you for being part of the FreeAPI community and contributing to the project! We look forward to featuring your fantastic work. 🌟
109+
110+
Happy coding! 🚀

README.md

+72-38
Original file line numberDiff line numberDiff line change
@@ -77,44 +77,6 @@ This ensures that your work is not affected by the server resets and allows you
7777
To self-host the FreeAPI.app application, you can take advantage of a pre-built template that is readily available.
7878
[Click here for detailed docs](https://github.com/hiteshchoudhary/apihub/#-using-railway-one-click-deploy)
7979

80-
# How to contribute - Guidelines
81-
82-
## Contribute in core FreeAPI codebase:
83-
84-
We welcome your interest in contributing to our open source project!
85-
86-
To contribute to FreeAPI, please follow these steps:
87-
88-
1. Fork the repository.
89-
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`.
90-
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).
91-
4. Push your changes to your forked repository: `git push origin feat/your-feature-name`.
92-
5. Submit a pull request to the main repository, explaining the changes you've made and providing any necessary details.
93-
94-
Here's a guide on how you can effectively contribute to our API hub:
95-
96-
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.
97-
98-
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.
99-
100-
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.
101-
102-
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.
103-
104-
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.
105-
106-
We appreciate your enthusiasm and look forward to your valuable contributions to our open source API hub project.
107-
108-
Together, we can foster a collaborative environment and make a significant impact in the API integration landscape.
109-
110-
Click [here](https://github.com/hiteshchoudhary/apihub/blob/main/CONTRIBUTING.md) for detailed contribution guide.
111-
112-
## Contribute by creating frontend application:
113-
114-
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.
115-
116-
Click [here](https://github.com/hiteshchoudhary/apihub/blob/main/CONTRIBUTING_FRONTEND.md) for detailed contribution guide for Frontend Developers 🚀!
117-
11880
# 🏁 Installation
11981

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

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

158+
# 🧪 Testing
159+
160+
To ensure reliability & stability for our end users, we utilize Playwright, a powerful testing framework to automate testing across all endpoints.
161+
162+
### 💻 Run the Test Server
163+
164+
Make sure to add `MONGO_MEMORY_SERVER_PORT=10000` (mongodb port for e2e testing) in your `.env` file.
165+
166+
```bash
167+
yarn start:test-server
168+
```
169+
170+
### 🧪 Run Tests
171+
172+
```bash
173+
yarn test:playwright
174+
```
175+
176+
This will generate a Playwright report. To view this report run the following command
177+
178+
```bash
179+
yarn playwright show-report
180+
```
181+
182+
Make sure all the test cases are passed whenever you make any changes.
183+
184+
# How to contribute - Guidelines
185+
186+
## ⚡️ Contribute in core FreeAPI codebase:
187+
188+
We welcome your interest in contributing to our open source project!
189+
190+
To contribute to FreeAPI, please follow these steps:
191+
192+
1. Fork the repository.
193+
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.
194+
3. Run all the tests 🧪 before committing the changes and make sure all tests are passed.
195+
4. After all tests are passed, commit your changes with a descriptive messages: `git commit -am 'add your commit message'`
196+
5. For more details on the commit format and other guidelines, please refer to the [Contributor Guidelines](./CONTRIBUTING.md).
197+
6. Push your changes to your forked repository: `git push origin feat/your-feature-name`.
198+
7. Submit a pull request to the main repository, explaining the changes you've made and providing any necessary details.
199+
200+
Here's a guide on how you can effectively contribute to our API hub:
201+
202+
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.
203+
204+
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.
205+
206+
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.
207+
208+
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.
209+
210+
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.
211+
212+
We appreciate your enthusiasm and look forward to your valuable contributions to our open source API hub project.
213+
214+
Together, we can foster a collaborative environment and make a significant impact in the API integration landscape.
215+
216+
Click [here](https://github.com/hiteshchoudhary/apihub/blob/main/CONTRIBUTING.md) for detailed contribution guide.
217+
218+
## 🚀 Contribute by creating frontend application:
219+
220+
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.
221+
222+
Click [here](https://github.com/hiteshchoudhary/apihub/blob/main/CONTRIBUTING_FRONTEND.md) for detailed contribution guide for Frontend Developers 🚀!
223+
224+
## 🧪 Contribute in testing suite
225+
226+
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.
227+
228+
Click [here](https://github.com/hiteshchoudhary/apihub/blob/main/CONTRIBUTING_CODE_COVERAGE.md) for detailed contribution guide for increasing code coverage.
229+
196230
# 📜 Swagger Docs
197231

198232
[Swagger Docs](https://api.freeapi.app): https://api.freeapi.app

e2e/common.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import dotenv from "dotenv";
2+
3+
dotenv.config({
4+
path: "../.env",
5+
});
6+
7+
export const URL = `http://localhost:${process.env.PORT || 8080}`;
8+
9+
export const getApiContext = async (playwright) =>
10+
playwright.request.newContext({
11+
baseURL: URL,
12+
});

e2e/db.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import mongoose, { mongo } from "mongoose";
2+
import { MongoMemoryServer } from "mongodb-memory-server";
3+
4+
const MONGO_MEMORY_SERVER_PORT = process.env.MONGO_MEMORY_SERVER_PORT || 10000;
5+
const MONGODB_URL = `mongodb://127.0.0.1:${MONGO_MEMORY_SERVER_PORT}/`;
6+
7+
let mongoServer = null;
8+
let dbInstance = undefined;
9+
10+
const connectDB = async () => {
11+
try {
12+
await mongoose.disconnect();
13+
mongoServer = await MongoMemoryServer.create({
14+
instance: {
15+
port: +MONGO_MEMORY_SERVER_PORT,
16+
},
17+
});
18+
dbInstance = await mongoose.connect(MONGODB_URL);
19+
} catch (error) {
20+
console.error("Mongo db connect error: ", error);
21+
process.exit(1);
22+
}
23+
};
24+
export const clearDB = async (collectionName = null) => {
25+
if (!dbInstance) {
26+
dbInstance = await mongoose.connect(MONGODB_URL);
27+
}
28+
const connection = mongoose.connection;
29+
if (collectionName) {
30+
await connection.db.collection(collectionName).deleteMany({});
31+
} else {
32+
const collections = await connection.db.listCollections().toArray();
33+
const collectionNames = collections.map((col) => col.name);
34+
for (let name of collectionNames) {
35+
await connection.db.collection(name).deleteMany({});
36+
}
37+
}
38+
};
39+
40+
export default connectDB;

0 commit comments

Comments
 (0)