i have a express app i am creating in typescript. I also am using typeorm as my orm for connecting to the database and actioning on it.
I already have jest unit tests but i also wanted to create integration tests with my database. I was using postgreSQL but i dont want to actually connect to my actual db every time i run the database. With this in mind i wanted to use a in memory db just for testing purposes.
typeorm and sqlite work very well for in memory db support but i found pg-mem for postgresql but cant find out how to really integrate it and any help would be useful.
i had my data-source file created as:
import { DataSource } from 'typeorm';
import { MysqlConnectionOptions } from 'typeorm/driver/mysql/MysqlConnectionOptions';
import logger from '@common/utils/logger';
export const AppDataSource = new DataSource({
type: process.env.DB_TYPE as MysqlConnectionOptions['type'],
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT || '5432', 10),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
synchronize: true,
logging: true,
logger: 'file',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
migrationsTableName: 'migrations',
migrations: ['migrations/*{.ts,.js}']
});
export const initializeDataSource = async (): Promise<void> => {
const fN = 'initializeDataSource';
try {
await AppDataSource.initialize();
logger.info(`${fN} - Data Source has been initialized!`);
} catch (err) {
logger.error(`${fN} - Error during Data Source initialization:`, err);
throw err;
}
};
and then in my app file i start the server and db by:
AppDataSource.initialize()
.then(async () => {
const PORT = process.env.PORT || '3001';
app.listen(PORT, () => {
logger.info(`Server is running on port ${PORT}`);
});
})
.catch(error => logger.error('Database connection error: ', error));
but now how i can utilize jest to mock that to use pg-mem i cant figure it out. any help would be greatly appreciated.
Any other suggestions i am open to as well