diff --git a/src/tests/integration/services/productType.integration.spec.ts b/src/tests/integration/services/productType.integration.spec.ts index e5bac29..49f46f5 100644 --- a/src/tests/integration/services/productType.integration.spec.ts +++ b/src/tests/integration/services/productType.integration.spec.ts @@ -1,4 +1,4 @@ -import testDataSource from "../../../config/ormconfig.test"; +import AppDataSource from "../../../config/ormconfig"; import { ProductVariant } from "../../../entities/ProductVariant"; import { Product } from "../../../entities/Product"; import { ProductVariantService } from "../../../services/productVariant.service"; @@ -7,30 +7,30 @@ describe("ProductVariantService Integration Tests", () => { let service: ProductVariantService; beforeAll(async () => { - if (!testDataSource.isInitialized) { - await testDataSource.initialize(); + if (!AppDataSource.isInitialized) { + await AppDataSource.initialize(); } - await testDataSource.synchronize(true); + await AppDataSource.synchronize(true); service = new ProductVariantService(); }); afterEach(async () => { - const entities = testDataSource.entityMetadatas; + const entities = AppDataSource.entityMetadatas; for (const entity of entities) { - const repository = testDataSource.getRepository(entity.name); + const repository = AppDataSource.getRepository(entity.name); await repository.clear(); } }); afterAll(async () => { - if (testDataSource.isInitialized) { - await testDataSource.destroy(); + if (AppDataSource.isInitialized) { + await AppDataSource.destroy(); } }); it("should create a ProductVariant with Product", async () => { - const productRepo = testDataSource.getRepository(Product); + const productRepo = AppDataSource.getRepository(Product); const product = productRepo.create({ name: "Laptop", @@ -53,11 +53,11 @@ describe("ProductVariantService Integration Tests", () => { }); it("should fetch all ProductVariants with their Products", async () => { - const productRepo = testDataSource.getRepository(Product); + const productRepo = AppDataSource.getRepository(Product); const product = productRepo.create({ name: "Laptop" }); const savedProduct = await productRepo.save(product); - const variantRepo = testDataSource.getRepository(ProductVariant); + const variantRepo = AppDataSource.getRepository(ProductVariant); const variant1 = variantRepo.create({ sku: "LAP123", price: 999.99, stock: 10, product: savedProduct }); const variant2 = variantRepo.create({ sku: "LAP456", price: 899.99, stock: 5, product: savedProduct }); @@ -76,11 +76,11 @@ describe("ProductVariantService Integration Tests", () => { }); it("should delete a ProductVariant", async () => { - const productRepo = testDataSource.getRepository(Product); + const productRepo = AppDataSource.getRepository(Product); const product = productRepo.create({ name: "Laptop" }); const savedProduct = await productRepo.save(product); - const variantRepo = testDataSource.getRepository(ProductVariant); + const variantRepo = AppDataSource.getRepository(ProductVariant); const variant = variantRepo.create({ sku: "LAP123", price: 999.99, stock: 10, product: savedProduct }); const savedVariant = await variantRepo.save(variant); diff --git a/src/tests/integration/services/productVariantService.integration.spec.ts b/src/tests/integration/services/productVariantService.integration.spec.ts index 7758d1f..703c9e8 100644 --- a/src/tests/integration/services/productVariantService.integration.spec.ts +++ b/src/tests/integration/services/productVariantService.integration.spec.ts @@ -1,4 +1,4 @@ -import testDataSource from "../../../config/ormconfig.test"; +import AppDataSource from "../../../config/ormconfig"; import { Product } from "../../../entities/Product"; import { ProductType } from "../../../entities/ProductType"; import { ProductVariant } from "../../../entities/ProductVariant"; @@ -8,31 +8,31 @@ describe("ProductVariantService Integration Tests", () => { let service: ProductVariantService; beforeAll(async () => { - if (!testDataSource.isInitialized) { - await testDataSource.initialize(); + if (!AppDataSource.isInitialized) { + await AppDataSource.initialize(); } - await testDataSource.synchronize(true); + await AppDataSource.synchronize(true); service = new ProductVariantService(); }); afterAll(async () => { - if (testDataSource.isInitialized) { - await testDataSource.destroy(); + if (AppDataSource.isInitialized) { + await AppDataSource.destroy(); } }); afterEach(async () => { - const entities = testDataSource.entityMetadatas; + const entities = AppDataSource.entityMetadatas; for (const entity of entities) { - const repository = testDataSource.getRepository(entity.name); + const repository = AppDataSource.getRepository(entity.name); await repository.clear(); } }); it("should create a Product with a ProductType", async () => { - const productTypeRepo = testDataSource.getRepository(ProductType); - const productRepo = testDataSource.getRepository(Product); + const productTypeRepo = AppDataSource.getRepository(ProductType); + const productRepo = AppDataSource.getRepository(Product); const productType = await productTypeRepo.save({ name: "Electronics", description: "..." }); expect(productType.id).toBeDefined(); @@ -44,8 +44,8 @@ describe("ProductVariantService Integration Tests", () => { it("should create a ProductVariant with Product", async () => { - const productTypeRepo = testDataSource.getRepository(ProductType); - const productRepo = testDataSource.getRepository(Product); + const productTypeRepo = AppDataSource.getRepository(ProductType); + const productRepo = AppDataSource.getRepository(Product); const productType = productTypeRepo.create({ name: "Electronics", @@ -80,8 +80,8 @@ describe("ProductVariantService Integration Tests", () => { ); }); it("should fetch ProductVariants associated with a Product", async () => { - const productRepo = testDataSource.getRepository(Product); - const variantRepo = testDataSource.getRepository(ProductVariant); + const productRepo = AppDataSource.getRepository(Product); + const variantRepo = AppDataSource.getRepository(ProductVariant); const product = productRepo.create({ name: "Laptop" }); const savedProduct = await productRepo.save(product); diff --git a/src/tests/services/productService.spec.ts b/src/tests/services/productService.spec.ts index 8e84141..03797ab 100644 --- a/src/tests/services/productService.spec.ts +++ b/src/tests/services/productService.spec.ts @@ -2,7 +2,7 @@ import { ProductService } from '../../services/product.service'; import { Product } from '../../entities/Product'; import { Repository } from 'typeorm'; import { ProductType } from '../../entities/ProductType'; -import testDataSource from '../../config/ormconfig.test'; +import AppDataSource from '../../config/ormconfig'; jest.mock('../../config/ormconfig', () => ({ getRepository: jest.fn(), @@ -22,7 +22,7 @@ jest.mock('../../config/ormconfig', () => ({ create: jest.fn(), } as unknown as jest.Mocked>; - (testDataSource.getRepository as jest.Mock).mockReturnValue(mockRepo); + (AppDataSource.getRepository as jest.Mock).mockReturnValue(mockRepo); service = new ProductService(); }); @@ -39,7 +39,7 @@ jest.mock('../../config/ormconfig', () => ({ }; const productTypeRepo = { findOne: jest.fn().mockResolvedValue(productType) }; - (testDataSource.getRepository as jest.Mock) + (AppDataSource.getRepository as jest.Mock) .mockReturnValueOnce(productTypeRepo) .mockReturnValueOnce(mockRepo); diff --git a/src/tests/services/productTypeService.spec.ts b/src/tests/services/productTypeService.spec.ts index 101f408..0bba416 100644 --- a/src/tests/services/productTypeService.spec.ts +++ b/src/tests/services/productTypeService.spec.ts @@ -1,7 +1,7 @@ import { ProductTypeService } from '../../services/productType.service'; import { ProductType } from '../../entities/ProductType'; import { Repository } from 'typeorm'; -import testDataSource from '../../config/ormconfig.test'; +import AppDataSource from '../../config/ormconfig'; jest.mock('../../config/ormconfig', () => ({ getRepository: jest.fn(), @@ -21,7 +21,7 @@ jest.mock('../../config/ormconfig', () => ({ create: jest.fn(), } as unknown as jest.Mocked>; - (testDataSource.getRepository as jest.Mock).mockReturnValue(mockRepo); + (AppDataSource.getRepository as jest.Mock).mockReturnValue(mockRepo); service = new ProductTypeService(); }); diff --git a/src/tests/services/productVariantService.spec.ts b/src/tests/services/productVariantService.spec.ts index 3684406..2552477 100644 --- a/src/tests/services/productVariantService.spec.ts +++ b/src/tests/services/productVariantService.spec.ts @@ -1,7 +1,7 @@ import { ProductVariantService } from '../../services/productVariant.service'; import { ProductVariant } from '../../entities/ProductVariant'; import { Repository } from 'typeorm'; -import testDataSource from '../../config/ormconfig.test'; +import AppDataSource from '../../config/ormconfig'; import { ProductType } from '../../entities/ProductType'; import { Product } from '../../entities/Product'; @@ -28,7 +28,7 @@ describe('ProductVariantService', () => { } as any, } as unknown as jest.Mocked>; - (testDataSource.getRepository as jest.Mock).mockReturnValue(mockRepo); + (AppDataSource.getRepository as jest.Mock).mockReturnValue(mockRepo); service = new ProductVariantService(); (service as any).repository = mockRepo; @@ -42,7 +42,7 @@ describe('ProductVariantService', () => { const savedVariant = { id: 1, ...variantData, product: productData, createdAt: new Date() }; const productRepo = { findOne: jest.fn().mockResolvedValue(productData) }; - (testDataSource.getRepository as jest.Mock) + (AppDataSource.getRepository as jest.Mock) .mockReturnValueOnce(productRepo) .mockReturnValueOnce(mockRepo); diff --git a/src/tests/services/testService.spec.ts b/src/tests/services/testService.spec.ts index 2555073..9bc1b87 100644 --- a/src/tests/services/testService.spec.ts +++ b/src/tests/services/testService.spec.ts @@ -1,5 +1,5 @@ import { setupTestDB, teardownTestDB } from "../../utils/test-utils"; -import testDataSource from "../../config/ormconfig.test"; +import { testDataSource } from "../../config/ormconfig.test"; import { TestService } from "../../services/test.service"; import { TestEntity } from "../../entities/testEntity"; diff --git a/src/tests/services/userService.spec.ts b/src/tests/services/userService.spec.ts index 794169c..7e90ddc 100644 --- a/src/tests/services/userService.spec.ts +++ b/src/tests/services/userService.spec.ts @@ -1,5 +1,5 @@ import { setupTestDB, teardownTestDB } from "../../utils/test-utils"; -import testDataSource from "../../config/ormconfig.test"; +import { testDataSource } from "../../config/ormconfig.test"; import { UserService } from "../../services/User.service"; import { User } from "../../entities/User"; diff --git a/src/tests/setup.ts b/src/tests/setup.ts index ce3e597..f194730 100644 --- a/src/tests/setup.ts +++ b/src/tests/setup.ts @@ -1,23 +1,29 @@ -import testDataSource from "../config/ormconfig.test"; +import AppDataSource from "../config/ormconfig"; beforeAll(async () => { - if (!testDataSource.isInitialized) { - await testDataSource.initialize(); + if (!AppDataSource.isInitialized) { + await AppDataSource.initialize(); } - await testDataSource.synchronize(); + if (process.env.NODE_ENV === "test") { + await AppDataSource.synchronize(); + + const queryRunner = AppDataSource.createQueryRunner(); + const tables = await queryRunner.getTables(); + await queryRunner.release(); + } }); afterEach(async () => { - const entities = testDataSource.entityMetadatas; + const entities = AppDataSource.entityMetadatas; for (const entity of entities) { - const repository = testDataSource.getRepository(entity.name); + const repository = AppDataSource.getRepository(entity.name); await repository.clear(); } }); afterAll(async () => { - if (testDataSource.isInitialized) { - await testDataSource.destroy(); + if (AppDataSource.isInitialized) { + await AppDataSource.destroy(); } });