@neoma/managed-database
Version:
A managed database fixture for Jest testing (unit, integration etc)
38 lines • 1.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.managedDatasourceInstance = exports.datasource = void 0;
const typeorm_1 = require("typeorm");
/**
* Create a new datasource for testing that uses an in-memory SQLite database.
*
* @returns An initialized datasource.
*/
const datasource = async () => {
const dataSource = new typeorm_1.DataSource({
type: "sqlite",
database: ":memory:",
entities: ["src/**/*.entity.ts"],
synchronize: true,
});
return dataSource.initialize();
};
exports.datasource = datasource;
let datasourceInstance;
beforeEach(async () => {
datasourceInstance = await (0, exports.datasource)();
});
afterEach(async () => {
await datasourceInstance.destroy();
});
/**
* Convenience function to get the Datasource instance for testing.
*
* Note: The datasource instance's lifecycle is managed by beforeEach and
* afterEach hooks that are automatically added to Jest when this module is
* imported.
*
* @returns A ephemeral DataSource instance for any tests that require a database.
*/
const managedDatasourceInstance = () => datasourceInstance;
exports.managedDatasourceInstance = managedDatasourceInstance;
//# sourceMappingURL=index.js.map