tududi/backend/tests/helpers/setup.js
Antonis Anastasiadis 297600e3c8
Config fixes (#100)
* New file with all configuration parameters.

* Remove all env var usage and use new config.
2025-07-01 13:29:12 +03:00

46 lines
1.2 KiB
JavaScript

// Set test environment before importing models
process.env.NODE_ENV = 'test';
const { sequelize } = require('../../models');
beforeAll(async () => {
// Ensure test database is clean and created
await sequelize.sync({ force: true });
}, 30000);
beforeEach(async () => {
// Clean all tables except Sessions to avoid conflicts
try {
const models = Object.values(sequelize.models);
const nonSessionModels = models.filter(
(model) => model.name !== 'Session'
);
await Promise.all(
nonSessionModels.map((model) =>
model.destroy({ truncate: true, cascade: true })
)
);
} catch (error) {
// Ignore errors during cleanup
}
});
afterEach(async () => {
// Clean up sessions after each test
try {
const Session = sequelize.models.Session;
if (Session) {
await Session.destroy({ truncate: true });
}
} catch (error) {
// Ignore errors during session cleanup
}
});
afterAll(async () => {
try {
await sequelize.close();
} catch (error) {
// Database may already be closed
}
}, 30000);