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