This directory contains integration tests for the Express MongoDB application. Unlike unit tests, these tests make actual HTTP requests to the Express app using supertest and connect to a real MongoDB instance to verify end-to-end functionality.
The integration tests are organized into three main categories:
-
Movie CRUD Integration Tests (
movie.integration.test.ts)- Tests basic CRUD API endpoints using
supertest - Makes actual HTTP requests to the Express app
- Tests pagination, filtering, and sorting via API
- Automatically skipped if
MONGODB_URIis not set
- Tests basic CRUD API endpoints using
-
MongoDB Search Integration Tests (
mongodbSearch.integration.test.ts)- Tests the MongoDB Atlas Search API endpoint using
supertest - Makes actual HTTP requests to test the
/api/movies/searchendpoint - Tests search by plot, directors, cast, pagination, and search operators
- Automatically skipped if
ENABLE_SEARCH_TESTSis not set
- Tests the MongoDB Atlas Search API endpoint using
-
Advanced Endpoints Integration Tests (
advancedEndpoints.integration.test.ts)- Tests advanced API endpoints using
supertest - Makes actual HTTP requests to test:
- Aggregation endpoints: Movies with comments, statistics by year, directors with most movies
- Atlas Search endpoint: Compound queries, phrase matching, fuzzy matching
- Vector Search endpoint: Semantic similarity search using embeddings
- Aggregation tests automatically run if
MONGODB_URIis set - Atlas Search tests require
ENABLE_SEARCH_TESTS=true - Vector Search tests require
VOYAGE_API_KEYto be set
- Tests advanced API endpoints using
Note: Tests use describeIntegration, describeSearch, and describeVectorSearch wrappers (from setup.ts and test files) that automatically skip entire test suites when the required environment variables are not set.
These integration tests use supertest to make actual HTTP requests to the Express application, testing the complete request/response cycle including:
- Routing
- Request parsing
- Controller logic
- Database operations
- Response formatting
- Error handling
This approach ensures that the API endpoints work correctly from the client's perspective.
- MONGODB_URI environment variable must be set
- MongoDB instance must be accessible (can be local MongoDB or Atlas)
- MongoDB instance with Search enabled (local MongoDB or Atlas)
- MONGODB_URI environment variable
- ENABLE_SEARCH_TESTS=true environment variable to enable tests
- MONGODB_URI environment variable must be set
- VOYAGE_API_KEY environment variable must be set with a valid Voyage AI API key
- MongoDB instance must have the
embedded_moviescollection with vector embeddings - Vector search index must be configured on the
embedded_moviescollection
npm run test:integration# Run only CRUD tests
npx jest --config jest.integration.config.json tests/integration/movie.integration.test.ts
# Run only Search tests (requires Search-enabled MongoDB)
npx jest --config jest.integration.config.json tests/integration/mongodbSearch.integration.test.ts
# Run only Advanced Endpoints tests (aggregations, search, vector search)
npx jest --config jest.integration.config.json tests/integration/advancedEndpoints.integration.test.tsCreate a .env file in the server/js-express directory:
MONGODB_URI=mongodb://localhost:27017/sample_mflix
# or for Atlas
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/sample_mflix?retryWrites=true&w=majority
# Optional: Enable Search tests (requires Search-enabled MongoDB)
ENABLE_SEARCH_TESTS=true
# Optional: Enable Vector Search tests (requires Voyage AI API key)
VOYAGE_API_KEY=your-voyage-ai-api-keyAll integration tests follow an idempotent pattern to ensure they can be run multiple times without side effects:
- Global Setup (
beforeAllinsetup.ts): Connects to MongoDB once before all tests - Global Teardown (
afterAllinsetup.ts): Closes MongoDB connection after all tests - Pre-Test Cleanup (
beforeAllin test files): Removes any orphaned test data from previous runs - Post-Test Cleanup (
afterEachin test files): Removes test data created during each test
This ensures:
- Tests can be run multiple times without conflicts
- The dataset is left in an untouched state after test execution
- Tests are isolated from each other
- Failed test runs don't affect subsequent runs
If you see "
- Make sure
MONGODB_URIenvironment variable is set - Verify the connection string is correct
- Check that MongoDB is running and accessible
If you see "
- Set
ENABLE_SEARCH_TESTS=trueenvironment variable - Verify your MongoDB instance has Search enabled (available in both local MongoDB and Atlas)
- For local MongoDB, ensure you're running a version that supports Search
If you see "
- Set
VOYAGE_API_KEYenvironment variable with your Voyage AI API key - Verify your MongoDB instance has the
embedded_moviescollection - Ensure the vector search index is configured on the
embedded_moviescollection - The
embedded_moviescollection should have documents withplot_embedding_voyage_3_largefield
If tests fail with connection errors:
- Verify
MONGODB_URIis correct - Check that MongoDB is running and accessible
- For Atlas: Verify your IP address is whitelisted
- For Atlas: Verify database user credentials are correct
- For local MongoDB: Verify the connection string format is correct
If search tests fail with "Search index did not become ready within 120 seconds":
- Check that your MongoDB instance has Search enabled
- Verify the search index is being created (check logs or admin UI)
- The index creation time varies by instance type and data size
- For Atlas free tier clusters, index creation may take longer
- For local MongoDB, ensure Search is properly configured
Integration tests have a 2-minute timeout (120 seconds) to accommodate:
- Network latency
- Index creation and polling
- Document indexing delays
If tests timeout, you may need to:
- Check your network connection
- Use a faster MongoDB instance
- Increase the timeout in
jest.integration.config.json
| Aspect | Unit Tests | Integration Tests |
|---|---|---|
| HTTP Requests | Mocked with Jest | Real HTTP requests via supertest |
| Database | Mocked with Jest | Real MongoDB instance |
| Speed | Fast (milliseconds) | Slower (seconds) |
| Dependencies | None | Requires MongoDB |
| Isolation | Complete | Requires cleanup |
| CI/CD | Always run | Conditional (requires MONGODB_URI) |
| Purpose | Test business logic | Test end-to-end API functionality |