This is the complete white-box testing implementation for the IDURAR ERP/CRM backend system. The test suite uses Jest as the testing framework and Supertest for API integration testing, with MongoDB Memory Server for isolated test database instances.
Tested By: Arsal (Backend Testing Lead)
Project: Software Quality Engineering Course Project
Application: IDURAR ERP/CRM Open Source
Framework: Jest + Supertest + MongoDB Memory Server
Target Coverage: β₯ 85%
- β Unit Tests - Test individual functions, methods, and modules (white-box testing)
- β Integration Tests - Test API endpoints with database interactions
- β High Coverage - Achieve minimum 85% code coverage across lines, branches, functions, and statements
- β CI/CD Integration - Automated testing via GitHub Actions
- β Defect Tracking - Document bugs discovered during testing
backend/
βββ tests/
β βββ README.md # This file - main testing documentation
β βββ setup/ # Test configuration and setup
β β βββ globalSetup.js # MongoDB Memory Server initialization
β β βββ globalTeardown.js # Clean up after all tests
β β βββ setupTests.js # Per-test-file setup (DB connection, cleanup)
β β βββ setup.test.js # Smoke test for test environment
β β
β βββ helpers/ # Test utilities and helper functions
β β βββ authHelper.js # Authentication helpers (create users, tokens)
β β βββ dbHelper.js # Database utilities (clear, drop, ObjectId)
β β βββ factories.js # Data factories for test objects
β β βββ requestHelper.js # Supertest request helpers
β β
β βββ unit/ # Unit tests (white-box)
β β βββ README.md # Unit test documentation
β β
β βββ integration/ # Integration tests (API + DB)
β β βββ README.md # Integration test documentation
β β
β βββ coverage/ # Coverage reports (auto-generated)
β βββ README.md # Coverage documentation
β
βββ jest.config.js # Jest configuration
βββ .env.test # Test environment variables
βββ package.json # Updated with test scripts
- Node.js v20.9.0 or higher
- npm v10.2.4 or higher
- MongoDB (Not required - uses in-memory database)
Navigate to the backend directory and install all testing dependencies:
cd backend
npm installNew Testing Dependencies Added:
jest(^29.7.0) - Testing frameworksupertest(^6.3.4) - HTTP assertion librarymongodb-memory-server(^9.1.6) - In-memory MongoDB for isolated tests@shelf/jest-mongodb(^4.3.2) - Jest preset for MongoDBcross-env(^7.0.3) - Cross-platform environment variables
The test suite uses .env.test for test-specific configuration:
NODE_ENV=test
DATABASE=mongodb://localhost:27017/idurar_test
JWT_SECRET=test_jwt_secret_key_for_testing_only
PORT=8889Important: Tests use MongoDB Memory Server, so no external MongoDB instance is required. The DATABASE variable is overridden at runtime.
Key settings in jest.config.js:
- Test Environment: Node.js
- Coverage Threshold: 85% (lines, branches, functions, statements)
- Test Timeout: 30 seconds per test
- Coverage Directory:
tests/coverage/ - Module Alias:
@/maps tosrc/(same as production)
Add these scripts to run tests (already configured in package.json):
{
"scripts": {
"test": "cross-env NODE_ENV=test jest --runInBand --detectOpenHandles --forceExit",
"test:watch": "cross-env NODE_ENV=test jest --watch --runInBand",
"test:coverage": "cross-env NODE_ENV=test jest --coverage --runInBand --detectOpenHandles --forceExit",
"test:unit": "cross-env NODE_ENV=test jest --testPathPattern=tests/unit --runInBand",
"test:integration": "cross-env NODE_ENV=test jest --testPathPattern=tests/integration --runInBand --detectOpenHandles --forceExit"
}
}npm testnpm run test:coveragenpm run test:unitnpm run test:integrationnpm run test:watchProvides helpers for creating authenticated test users:
const { createAuthenticatedUser, generateTestToken } = require('./helpers/authHelper');
// Create admin with token
const { admin, token, authHeader } = await createAuthenticatedUser();Database management functions:
const { clearDatabase, generateObjectId } = require('./helpers/dbHelper');
// Clear all collections
await clearDatabase();
// Generate valid MongoDB ObjectId
const id = generateObjectId();Create test data easily:
const { ClientFactory, InvoiceFactory } = require('./helpers/factories');
// Create single client
const client = await ClientFactory.create({ name: 'ACME Corp' });
// Create multiple clients
const clients = await ClientFactory.createMany(5);Supertest utilities for API testing:
const { authenticatedRequest } = require('./helpers/requestHelper');
// Make authenticated GET request
const response = await authenticatedRequest(app, 'get', '/api/clients', token);-
Global Setup (
globalSetup.js)- Starts MongoDB Memory Server before all tests
- Creates temporary in-memory database
- Stores connection URI for tests
-
Per-File Setup (
setupTests.js)- Runs before each test file
- Connects to test database
- Clears collections after each test
- Disconnects after all tests in file
-
Test Execution
- Unit tests run in isolation
- Integration tests use Supertest to simulate HTTP requests
- Database operations use real Mongoose models
-
Global Teardown (
globalTeardown.js)- Stops MongoDB Memory Server
- Cleans up temporary files
- Each test file gets a clean database state
- Collections are cleared after each test (
afterEach) - No test can affect another test's data
- No production/development database is ever touched
We aim for 85%+ coverage across:
- Lines: 85%
- Branches: 85%
- Functions: 85%
- Statements: 85%
collectCoverageFrom: [
'src/**/*.js',
'!src/server.js', // Exclude server entry point
'!src/setup/**', // Exclude setup scripts
'!src/public/**', // Exclude static files
'!src/emailTemplate/**', // Exclude email templates
'!src/pdf/**', // Exclude PDF templates
]Coverage reports are generated in multiple formats:
- Terminal Output: Text summary
- HTML Report:
tests/coverage/lcov-report/index.html - LCOV:
tests/coverage/lcov.info(for CI/CD tools) - JSON Summary:
tests/coverage/coverage-summary.json
Completed Tasks:
- β Jest configuration with proper settings
- β MongoDB Memory Server integration
- β Test environment variables (.env.test)
- β Global setup and teardown scripts
- β Per-test-file setup with database cleanup
- β Test helper utilities (auth, db, factories, requests)
- β Test directory structure created
- β Package.json updated with test scripts
- β Smoke test to verify environment
Verification:
Run the smoke test to verify setup:
npm test -- tests/setup/setup.test.jsExpected output:
β
MongoDB Memory Server started
β
Connected to test database
β
All setup tests pass
β
Disconnected from test database
β
MongoDB Memory Server stopped
- Write 10-12 unit tests for Auth & Customer modules
- Target: 80%+ coverage for tested modules
- Documentation:
tests/unit/README.md
- Write 12-15 API integration tests
- Cover full request/response cycles
- Documentation:
tests/integration/README.md
- Expand test coverage to meet threshold
- Generate coverage report with screenshots
- Documentation:
tests/coverage/README.md
- Create GitHub Actions workflow
- Automate test execution on push/PR
- Documentation:
.github/workflows/README.md
- Track bugs found during testing
- Create defect table with severity/status
- Final summary report
- Email Testing: Email functionality is mocked (not actually sent)
- File Uploads: File upload tests use mock multipart data
- External APIs: OpenAI and AWS S3 calls are mocked
- PDF Generation: PDF creation is mocked in tests
These are intentional for isolated testing and will be documented in integration tests.
Tester: Arsal
Role: Backend Testing Lead
Course: Software Quality Engineering
Project: IDURAR ERP/CRM Testing
For questions about test implementation, refer to individual test file READMEs or comments within test code.
Last Updated: December 6, 2025
Version: 1.0.0
Status: Step 1 Complete β