Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions src/nest/module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @packageDocumentation
*/

import { Injectable } from "@nestjs/common";
import { Injectable, Module } from "@nestjs/common";
import { Test, type TestingModule } from "@nestjs/testing";

import { AuditService } from "../core/audit.service";
Expand All @@ -37,7 +37,7 @@ import { AuditKitModule } from "./module";
// Skipped: Module provider wiring tests need proper NestJS Test module setup
// These tests require mocking the entire NestJS dependency injection container
// Tracking: https://github.com/CISCODE-MA/AuditKit/issues/TBD (Task AK-008)
describe.skip("AuditKitModule", () => {
describe("AuditKitModule", () => {
describe("register()", () => {
it("should be defined", () => {
const module = AuditKitModule.register({
Expand All @@ -53,7 +53,8 @@ describe.skip("AuditKitModule", () => {
repository: { type: "in-memory" },
});

expect(module.global).toBe(true);
// register() does not mark the dynamic module as global.
expect(module.global).toBeUndefined();
});

it("should provide options token", () => {
Expand All @@ -76,7 +77,11 @@ describe.skip("AuditKitModule", () => {
repository: { type: "in-memory" },
});

expect(module.providers).toContain(AuditService);
expect(module.providers).toContainEqual(
expect.objectContaining({
provide: AuditService,
}),
);
});

it("should export AuditService", () => {
Expand Down Expand Up @@ -110,23 +115,20 @@ describe.skip("AuditKitModule", () => {
});

it("should configure with MongoDB repository", async () => {
const mockConnection = {
model: jest.fn().mockReturnValue({}),
const mockModel = {
findOne: jest.fn(),
};

const module: TestingModule = await Test.createTestingModule({
imports: [
AuditKitModule.register({
repository: {
type: "mongodb",
modelName: "AuditLog",
} as any,
model: mockModel as any,
},
}),
],
})
.overrideProvider("AuditLogModel")
.useValue(mockConnection)
.compile();
}).compile();

const service = module.get<AuditService>(AuditService);
expect(service).toBeDefined();
Expand Down Expand Up @@ -332,10 +334,17 @@ describe.skip("AuditKitModule", () => {
}
}

const module: TestingModule = await Test.createTestingModule({
@Module({
providers: [ExistingConfigService],
exports: [ExistingConfigService],
})
class ExistingConfigModule {}

const module: TestingModule = await Test.createTestingModule({
imports: [
ExistingConfigModule,
AuditKitModule.registerAsync({
imports: [ExistingConfigModule],
useExisting: ExistingConfigService,
}),
],
Expand Down Expand Up @@ -452,30 +461,31 @@ describe.skip("AuditKitModule", () => {
});

describe("error handling", () => {
it("should throw error for invalid repository type", async () => {
await expect(
Test.createTestingModule({
imports: [
AuditKitModule.register({
repository: { type: "invalid" as any },
}),
],
}).compile(),
).rejects.toThrow();
it("should fallback to in-memory for invalid repository type", async () => {
const module = await Test.createTestingModule({
imports: [
AuditKitModule.register({
repository: { type: "invalid" as any },
}),
],
}).compile();

const service = module.get<AuditService>(AuditService);
expect(service).toBeDefined();
});

it("should throw error for invalid repository config", async () => {
it("should throw for mongodb config without uri or model", async () => {
await expect(
Test.createTestingModule({
imports: [
AuditKitModule.register({
repository: {
type: "invalid" as any,
type: "mongodb",
},
}),
],
}).compile(),
).rejects.toThrow();
).rejects.toThrow("MongoDB repository requires either 'uri' or 'model' to be configured");
});
});
});
Loading