tractatus/tests/helpers/cleanup.js
TheFlow a5c41ac6ee fix: add Jest test infrastructure and reduce test failures from 29 to 13
- Add jest.config.js with test environment configuration
- Add tests/setup.js to load .env.test before tests
- Add tests/helpers/cleanup.js for test data cleanup utilities
- Add scripts/clean-test-db.js for manual test database cleanup
- Fix ObjectId constructor calls in api.admin.test.js (must use 'new')
- Add .env.test for test-specific configuration
- Use tractatus_prod database for tests (staging environment)

Test Results:
- Before: 29 failing tests (4 test suites)
- After: 13 failing tests (4 test suites)
- Progress: 16 test failures fixed (55% improvement)

Remaining Issues:
- 4 auth test failures (user creation/password mismatch)
- 4 documents test failures (duplicate keys)
- 2 admin moderation test failures
- 3 health check test failures (response structure)

🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-09 20:37:45 +13:00

79 lines
2.1 KiB
JavaScript

/**
* Test Cleanup Helper
* Provides utilities for cleaning up test data between runs
*/
const { MongoClient } = require('mongodb');
const config = require('../../src/config/app.config');
/**
* Clean all collections in the test database
* @returns {Promise<void>}
*/
async function cleanTestDatabase() {
const connection = await MongoClient.connect(config.mongodb.uri);
const db = connection.db(config.mongodb.db);
try {
// Only clean if we're in test environment
if (config.env !== 'test' || !config.mongodb.db.includes('test')) {
throw new Error('cleanTestDatabase() can only be used with test databases');
}
// Get all collections
const collections = await db.listCollections().toArray();
// Drop each collection
for (const collection of collections) {
await db.collection(collection.name).deleteMany({});
}
console.log(`✓ Cleaned ${collections.length} collections in ${config.mongodb.db}`);
} finally {
await connection.close();
}
}
/**
* Clean specific test documents by slug pattern
* @param {string} slugPattern - Pattern to match (e.g., 'test-document-integration')
* @returns {Promise<number>} - Number of documents deleted
*/
async function cleanTestDocuments(slugPattern) {
const connection = await MongoClient.connect(config.mongodb.uri);
const db = connection.db(config.mongodb.db);
try {
const result = await db.collection('documents').deleteMany({
slug: { $regex: slugPattern }
});
return result.deletedCount;
} finally {
await connection.close();
}
}
/**
* Clean specific test users by email pattern
* @param {string} emailPattern - Pattern to match (e.g., 'test@')
* @returns {Promise<number>} - Number of users deleted
*/
async function cleanTestUsers(emailPattern) {
const connection = await MongoClient.connect(config.mongodb.uri);
const db = connection.db(config.mongodb.db);
try {
const result = await db.collection('users').deleteMany({
email: { $regex: emailPattern }
});
return result.deletedCount;
} finally {
await connection.close();
}
}
module.exports = {
cleanTestDatabase,
cleanTestDocuments,
cleanTestUsers
};