Skip to content

Commit fae6987

Browse files
committed
Add integration tests for Express server
1 parent c55950b commit fae6987

File tree

8 files changed

+991
-2
lines changed

8 files changed

+991
-2
lines changed

server/express/.env.example

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ PORT=3001
77
NODE_ENV=development
88

99
# CORS Configuration (frontend URL)
10-
CORS_ORIGIN=http://localhost:3000
10+
CORS_ORIGIN=http://localhost:3000
11+
12+
# Optional: Enable MongoDB Search tests
13+
# Uncomment the following line to enable Search tests
14+
# ENABLE_SEARCH_TESTS=true

server/express/jest.config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
"**/__tests__/**/*.ts",
77
"**/?(*.)+(spec|test).ts"
88
],
9+
"testPathIgnorePatterns": [
10+
"/node_modules/",
11+
"/tests/integration/"
12+
],
913
"transform": {
1014
"^.+\\.ts$": "ts-jest"
1115
},
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"preset": "ts-jest",
3+
"testEnvironment": "node",
4+
"roots": ["<rootDir>/tests/integration"],
5+
"testMatch": [
6+
"**/integration/**/*.test.ts",
7+
"**/integration/**/*.spec.ts"
8+
],
9+
"transform": {
10+
"^.+\\.ts$": "ts-jest"
11+
},
12+
"setupFilesAfterEnv": ["<rootDir>/tests/integration/setup.ts"],
13+
"testTimeout": 60000
14+
}
15+

server/express/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"dev": "ts-node src/app.ts",
1313
"test": "jest",
1414
"test:watch": "jest --watch",
15-
"test:coverage": "jest --coverage"
15+
"test:coverage": "jest --coverage",
16+
"test:integration": "jest --config jest.integration.config.json"
1617
},
1718
"dependencies": {
1819
"cors": "^2.8.5",
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Integration Tests
2+
3+
This directory contains integration tests for the Express MongoDB application. Unlike unit tests, these tests connect to
4+
a real MongoDB instance to verify end-to-end functionality.
5+
6+
## Overview
7+
8+
The integration tests are organized into two main categories:
9+
10+
1. **Movie CRUD Integration Tests** (`movie.integration.test.ts`)
11+
- Tests basic database operations (Create, Read, Update, Delete)
12+
- Verifies MongoDB driver functionality with a real database
13+
- Tests pagination, filtering, and sorting
14+
- Automatically skipped if `MONGODB_URI` is not set
15+
16+
2. **MongoDB Search Integration Tests** (`mongodbSearch.integration.test.ts`)
17+
- Tests MongoDB Search functionality
18+
- Works with both local MongoDB and Atlas instances
19+
- Tests search index creation, polling, and search queries
20+
- Automatically skipped if `ENABLE_SEARCH_TESTS` is not set
21+
22+
**Note:** Tests use `describeIntegration` and `describeSearch` wrappers (from `setup.ts`) that automatically skip entire
23+
test suites when the required environment variables are not set.
24+
25+
## Requirements
26+
27+
### Basic Integration Tests
28+
29+
- **MONGODB_URI** environment variable must be set
30+
- MongoDB instance must be accessible (can be local MongoDB or Atlas)
31+
32+
### Search Tests
33+
34+
- **MongoDB instance** with Search enabled (local MongoDB or Atlas)
35+
- **MONGODB_URI** environment variable
36+
- **ENABLE_SEARCH_TESTS=true** environment variable to enable tests
37+
38+
## Running the Tests
39+
40+
### Run All Integration Tests
41+
42+
```bash
43+
npm run test:integration
44+
```
45+
46+
### Run Specific Test File
47+
48+
```bash
49+
# Run only CRUD tests
50+
npx jest --config jest.integration.config.json tests/integration/movie.integration.test.ts
51+
52+
# Run only Search tests (requires Search-enabled MongoDB)
53+
npx jest --config jest.integration.config.json tests/integration/mongodbSearch.integration.test.ts
54+
```
55+
56+
### Set Environment Variables
57+
58+
#### Using .env file
59+
60+
Create a `.env` file in the `server/express` directory:
61+
62+
```env
63+
MONGODB_URI=mongodb://localhost:27017/sample_mflix
64+
# or for Atlas
65+
MONGODB_URI=mongodb+srv://username:[email protected]/sample_mflix?retryWrites=true&w=majority
66+
67+
# Optional: Enable Search tests (requires Search-enabled MongoDB)
68+
ENABLE_SEARCH_TESTS=true
69+
```
70+
71+
## Test Structure
72+
73+
### Setup and Teardown
74+
75+
- **Global Setup** (`beforeAll` in `setup.ts`): Connects to MongoDB once before all tests
76+
- **Global Teardown** (`afterAll` in `setup.ts`): Closes MongoDB connection after all tests
77+
- **Test Cleanup** (`afterEach` in test files): Removes test data after each test to ensure isolation
78+
79+
## Troubleshooting
80+
81+
### Tests are Skipped
82+
83+
If you see "⚠️ Skipping integration tests - MONGODB_URI not set":
84+
85+
1. Make sure `MONGODB_URI` environment variable is set
86+
2. Verify the connection string is correct
87+
3. Check that MongoDB is running and accessible
88+
89+
### Search Tests are Skipped
90+
91+
If you see "⚠️ Skipping MongoDB Search tests - ENABLE_SEARCH_TESTS not set":
92+
93+
1. Set `ENABLE_SEARCH_TESTS=true` environment variable
94+
2. Verify your MongoDB instance has Search enabled (available in both local MongoDB and Atlas)
95+
3. For local MongoDB, ensure you're running a version that supports Search
96+
97+
### Connection Errors
98+
99+
If tests fail with connection errors:
100+
101+
1. Verify `MONGODB_URI` is correct
102+
2. Check that MongoDB is running and accessible
103+
3. For Atlas: Verify your IP address is whitelisted
104+
4. For Atlas: Verify database user credentials are correct
105+
5. For local MongoDB: Verify the connection string format is correct
106+
107+
### Index Creation Timeout
108+
109+
If search tests fail with "Search index did not become ready within 120 seconds":
110+
111+
1. Check that your MongoDB instance has Search enabled
112+
2. Verify the search index is being created (check logs or admin UI)
113+
3. The index creation time varies by instance type and data size
114+
4. For Atlas free tier clusters, index creation may take longer
115+
5. For local MongoDB, ensure Search is properly configured
116+
117+
### Test Timeouts
118+
119+
Integration tests have a 2-minute timeout (120 seconds) to accommodate:
120+
121+
- Network latency
122+
- Index creation and polling
123+
- Document indexing delays
124+
125+
If tests timeout, you may need to:
126+
127+
1. Check your network connection
128+
2. Use a faster MongoDB instance
129+
3. Increase the timeout in `jest.integration.config.json`
130+
131+
## Differences from Unit Tests
132+
133+
| Aspect | Unit Tests | Integration Tests |
134+
|--------|-----------|-------------------|
135+
| Database | Mocked with Jest | Real MongoDB instance |
136+
| Speed | Fast (milliseconds) | Slower (seconds) |
137+
| Dependencies | None | Requires MongoDB |
138+
| Isolation | Complete | Requires cleanup |
139+
| CI/CD | Always run | Conditional (requires MONGODB_URI) |
140+
| Purpose | Test business logic | Test database operations |

0 commit comments

Comments
 (0)