Skip to content

Commit bd14b56

Browse files
authored
test: add integration tests
1 parent 94dc8dd commit bd14b56

17 files changed

+1150
-52
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: "Integration Tests"
2+
on:
3+
pull_request:
4+
types: [opened, reopened, synchronize]
5+
push:
6+
branches:
7+
- master
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref_name }}
11+
cancel-in-progress: true
12+
13+
jobs:
14+
integration-test:
15+
runs-on: ubuntu-latest
16+
strategy:
17+
matrix:
18+
node: [18, 20, 22]
19+
name: Node ${{ matrix.node }} Integration Tests
20+
timeout-minutes: 5
21+
22+
services:
23+
mysql:
24+
image: mysql:8.0.33
25+
env:
26+
MYSQL_ROOT_PASSWORD: password
27+
MYSQL_DATABASE: serverless_mysql_test
28+
MYSQL_ROOT_HOST: "%"
29+
ports:
30+
- 3306:3306
31+
options: >-
32+
--health-cmd="mysqladmin ping"
33+
--health-interval=10s
34+
--health-timeout=5s
35+
--health-retries=3
36+
37+
steps:
38+
- name: "Checkout latest code"
39+
uses: actions/checkout@v3
40+
with:
41+
ref: ${{ github.event.pull_request.head.sha }}
42+
43+
- name: Set up node
44+
uses: actions/setup-node@v3
45+
with:
46+
node-version: ${{ matrix.node }}
47+
48+
- name: Install dependencies
49+
run: npm ci
50+
51+
- name: Run unit tests
52+
run: npm run test:unit
53+
54+
- name: Run integration tests
55+
run: npm run test:integration
56+
env:
57+
MYSQL_HOST: 127.0.0.1
58+
MYSQL_PORT: 3306
59+
MYSQL_DATABASE: serverless_mysql_test
60+
MYSQL_USER: root
61+
MYSQL_PASSWORD: password
62+
63+
- name: Run all tests with coverage
64+
if: matrix.node == 22
65+
run: npm run test-cov
66+
env:
67+
MYSQL_HOST: 127.0.0.1
68+
MYSQL_PORT: 3306
69+
MYSQL_DATABASE: serverless_mysql_test
70+
MYSQL_USER: root
71+
MYSQL_PASSWORD: password

.github/workflows/pull-request.yml renamed to .github/workflows/unit-tests.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
name: "CI"
1+
name: "Unit Tests"
22
on:
33
pull_request:
44
types: [opened, reopened, synchronize]
55
push:
66
branches:
77
- master
88

9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref_name }}
11+
cancel-in-progress: true
12+
913
jobs:
1014
test:
1115
runs-on: ubuntu-latest
@@ -29,8 +33,8 @@ jobs:
2933
npm list -g --depth 0
3034
- name: Install dependencies
3135
run: npm ci
32-
- name: Run tests
33-
run: npm run test
36+
- name: Run unit tests
37+
run: npm run test:unit
3438

3539
lint:
3640
name: "ESLint"

CONTRIBUTING.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Contributing to serverless-mysql
2+
3+
Thank you for considering contributing to serverless-mysql! This document provides guidelines and instructions for contributing to the project.
4+
5+
## Code of Conduct
6+
7+
Please be respectful and considerate of others when contributing to this project. We aim to foster an inclusive and welcoming community.
8+
9+
## How to Contribute
10+
11+
1. Fork the repository
12+
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
13+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
14+
4. Push to the branch (`git push origin feature/amazing-feature`)
15+
5. Open a Pull Request
16+
17+
## Development Setup
18+
19+
1. Clone the repository
20+
2. Install dependencies with `npm install`
21+
3. Set up MySQL for testing:
22+
- Option 1: Install MySQL locally
23+
- Option 2: Use Docker (recommended):
24+
```bash
25+
docker compose up -d
26+
```
27+
4. Run unit tests with `npm run test:unit`
28+
5. Run integration tests with `npm run test:integration` (requires MySQL)
29+
6. Or run all tests with Docker handling MySQL automatically:
30+
```bash
31+
npm run test:docker
32+
```
33+
34+
## Running Tests
35+
36+
The project includes both unit tests and integration tests to ensure everything works correctly.
37+
38+
### Unit Tests
39+
40+
Unit tests don't require any external dependencies and can be run with:
41+
42+
```bash
43+
npm run test:unit
44+
```
45+
46+
### Integration Tests
47+
48+
Integration tests require a MySQL database. You can use the included Docker Compose file to start a MySQL instance:
49+
50+
```bash
51+
# Start MySQL container
52+
docker compose up -d
53+
54+
# Run integration tests
55+
npm run test:integration
56+
57+
# Stop MySQL container when done
58+
docker compose down
59+
```
60+
61+
For convenience, you can use the provided script to run the integration tests with Docker:
62+
63+
```bash
64+
# This will start MySQL, run the tests, and stop MySQL automatically
65+
npm run test:integration:docker
66+
```
67+
68+
The script includes a watchdog process that will automatically terminate tests if they run for too long (60 seconds), which helps prevent hanging test processes.
69+
70+
You can also configure the MySQL connection using environment variables:
71+
72+
```bash
73+
MYSQL_HOST=127.0.0.1 MYSQL_PORT=3306 MYSQL_DATABASE=serverless_mysql_test MYSQL_USER=root MYSQL_PASSWORD=password npm run test:integration
74+
```
75+
76+
### Running All Tests
77+
78+
To run both unit and integration tests:
79+
80+
```bash
81+
npm test
82+
```
83+
84+
**Important:** The `npm test` command requires a running MySQL instance, as it runs both unit and integration tests. If you don't have MySQL running, the tests will fail with connection errors. Use one of these approaches:
85+
86+
1. Start MySQL manually before running tests:
87+
```bash
88+
docker compose up -d
89+
npm test
90+
docker compose down
91+
```
92+
93+
2. Use the Docker-managed test script instead, which handles MySQL automatically:
94+
```bash
95+
npm run test:docker
96+
```
97+
98+
If you want to run all tests with Docker handling the MySQL database:
99+
100+
```bash
101+
npm run test:docker
102+
```
103+
104+
This will run the unit tests first, and if they pass, it will run the integration tests with Docker.
105+
106+
### Test Coverage
107+
108+
To run tests with coverage reporting:
109+
110+
```bash
111+
npm run test-cov
112+
```
113+
114+
This will generate an HTML coverage report in the `coverage` directory.
115+
116+
## Test Structure
117+
118+
- Unit tests are located in `test/unit/*.spec.js`
119+
- Integration tests are located in `test/integration/*.spec.js`
120+
- Test helpers are in:
121+
- `test/unit/helpers/` - Helpers for unit tests (mocks, etc.)
122+
- `test/integration/helpers/` - Helpers for integration tests (database setup, etc.)
123+
124+
## Integration Test Environment
125+
126+
The integration tests use a MySQL 8.0 container with the following configuration:
127+
128+
- Host: 127.0.0.1
129+
- Port: 3306
130+
- Database: serverless_mysql_test
131+
- User: root
132+
- Password: password
133+
134+
The MySQL container is configured with:
135+
- Native password authentication
136+
- 1000 max connections
137+
- Extended wait timeout (28800 seconds)
138+
139+
This is configured in the `docker-compose.yml` file and used by the GitHub Actions workflow for CI.
140+
141+
## Continuous Integration
142+
143+
The project uses GitHub Actions for continuous integration. Two workflows are configured:
144+
145+
1. **Unit Tests** - Runs unit tests and linting on pull requests and pushes to master
146+
2. **Integration Tests** - Runs both unit and integration tests with a MySQL service container
147+
148+
Both workflows run on Node.js versions 18, 20, and 22 to ensure compatibility across supported versions.
149+
150+
## Pull Request Process
151+
152+
1. Ensure your code passes all tests and linting
153+
2. Update documentation if necessary
154+
3. The PR should work in all supported Node.js versions (currently 18, 20, 22)
155+
4. Your PR will be reviewed by maintainers who may request changes
156+
157+
## Coding Standards
158+
159+
- Follow the existing code style
160+
- Write tests for new features
161+
- Keep the code simple and maintainable
162+
- Document public APIs
163+
- Run `npm run lint` to check your code against our ESLint rules
164+
165+
## Connection Management
166+
167+
When working on connection management features, be particularly careful about:
168+
- Properly closing connections to prevent leaks
169+
- Handling timeouts and error conditions
170+
- Testing with concurrent connections
171+
- Ensuring compatibility with serverless environments
172+
173+
## License
174+
175+
By contributing to serverless-mysql, you agree that your contributions will be licensed under the project's MIT License.

docker-compose.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
services:
2+
mysql:
3+
image: mysql:8.0
4+
container_name: serverless-mysql-test-db
5+
environment:
6+
MYSQL_ROOT_PASSWORD: password
7+
MYSQL_DATABASE: serverless_mysql_test
8+
MYSQL_USER: testuser
9+
MYSQL_PASSWORD: testpassword
10+
# Allow connections from any host
11+
MYSQL_ROOT_HOST: "%"
12+
ports:
13+
- "3306:3306"
14+
command: >
15+
--default-authentication-plugin=mysql_native_password
16+
--max_connections=1000
17+
--wait_timeout=28800
18+
healthcheck:
19+
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
20+
timeout: 5s
21+
retries: 10

0 commit comments

Comments
 (0)