Improvement #218
Updated by Chakkaphon Noinang (Jay) 10 days ago
# 1.ลบ folder test
# 2.แก้ไฟล์ jest.config
```javascript
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
moduleFileExtensions: ["js", "json", "ts"],
rootDir: ".",
testRegex: String.raw`.*\.spec\.ts$`,
collectCoverage: true,
coverageDirectory: "coverage",
coverageReporters: ["text", "lcov"],
collectCoverageFrom: [
"src/**/*.ts",
"!src/main.ts",
"!src/app.module.ts",
"!src/**/*.module.ts",
"!src/**/*.dto.ts",
"!src/**/*.type.ts",
"!src/shared/logger/**",
"!src/**/*.constant.ts",
"!src/**/index.ts",
],
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
coveragePathIgnorePatterns: ["/node_modules/", "/dist/"],
clearMocks: true,
restoreMocks: true,
resetMocks: true,
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
"^@shared/(.*)$": "<rootDir>/src/shared/$1",
"^@modules/(.*)$": "<rootDir>/src/modules/$1",
"^@infrastructure/(.*)$": "<rootDir>/src/infrastructure/$1",
"^@config/(.*)$": "<rootDir>/src/config/$1",
},
};
```
# 3.เพิ่มไฟล์ pg.provider.spec.ts ที่ src/infrastructure/pg
```javascript
import { Test, TestingModule } from "@nestjs/testing";
import { Pool } from "pg";
import { DatabaseModule } from "./pg.provider";
import { AppEnv } from "../../../config/env";
jest.mock("pg", () => {
return {
Pool: jest.fn().mockImplementation(() => {
return {
query: jest.fn(),
connect: jest.fn(),
end: jest.fn(),
};
}),
};
});
jest.mock("../../../config/env", () => ({
AppEnv: {
DB_HOST: "localhost",
DB_PORT: 5432,
DB_USERNAME: "testuser",
DB_PASSWORD: "testpass",
DB_NAME: "testdb",
},
}));
describe("DatabaseModule", () => {
let module: TestingModule;
let poolInstance: Pool;
beforeEach(async () => {
jest.clearAllMocks();
module = await Test.createTestingModule({
imports: [DatabaseModule],
}).compile();
poolInstance = module.get<Pool>("PG_POOL");
});
afterEach(async () => {
if (module) {
await module.close();
}
});
describe("Module Configuration", () => {
it("should be defined", () => {
expect(module).toBeDefined();
});
it("should provide PG_POOL", () => {
expect(poolInstance).toBeDefined();
});
it("should create Pool instance with correct configuration", () => {
expect(Pool).toHaveBeenCalledWith({
host: AppEnv.DB_HOST,
port: AppEnv.DB_PORT,
user: AppEnv.DB_USERNAME,
password: AppEnv.DB_PASSWORD,
database: AppEnv.DB_NAME,
});
});
it("should create Pool with expected database host", () => {
expect(Pool).toHaveBeenCalledWith(
expect.objectContaining({
host: "localhost",
}),
);
});
it("should create Pool with expected database port", () => {
expect(Pool).toHaveBeenCalledWith(
expect.objectContaining({
port: 5432,
}),
);
});
it("should create Pool with expected database username", () => {
expect(Pool).toHaveBeenCalledWith(
expect.objectContaining({
user: "testuser",
}),
);
});
it("should create Pool with expected database password", () => {
expect(Pool).toHaveBeenCalledWith(
expect.objectContaining({
password: "testpass",
}),
);
});
it("should create Pool with expected database name", () => {
expect(Pool).toHaveBeenCalledWith(
expect.objectContaining({
database: "testdb",
}),
);
});
});
describe("Provider Exports", () => {
it("should export PG_POOL provider", () => {
const moduleRef = module.get<Pool>("PG_POOL");
expect(moduleRef).toBeDefined();
});
it("should have DatabaseModule defined", () => {
expect(DatabaseModule).toBeDefined();
});
});
describe("Pool Instance Methods", () => {
it("should return pool instance from module", () => {
expect(poolInstance).toBeDefined();
expect(poolInstance).toBeTruthy();
});
it("should be injectable as PG_POOL token", async () => {
const testModule = await Test.createTestingModule({
imports: [DatabaseModule],
}).compile();
const pool = testModule.get<Pool>("PG_POOL");
expect(pool).toBeDefined();
await testModule.close();
});
});
describe("Factory Function", () => {
it("should call useFactory when module is compiled", () => {
expect(Pool).toHaveBeenCalled();
});
it("should create only one Pool instance per module", () => {
const pool1 = module.get<Pool>("PG_POOL");
const pool2 = module.get<Pool>("PG_POOL");
expect(pool1).toBe(pool2);
});
});
});
```
# 4.เพิ่ม4ไฟล์ตามไฟล์ http-spec.zip และ recheck อีกครั้งว่าใน folder มีไฟล์ .ts เท่ากันไฟล์ .spec ไหม
- all-exceptions.filter.spec.ts
- response-envelope.interceptor.spec.ts
- response-envelope.spec.ts
- skip-envelope.decorator.spec.ts
# 5.เพิ่ม2ไฟล์ตามไฟล์ errors-spec.zip และ recheck อีกครั้งว่าใน folder มีไฟล์ .ts เท่ากันไฟล์ .spec ไหม
- app-error.spec.ts
- error-ext.spec.ts