Files
core/test/modules.test.ts

96 lines
2.2 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import "reflect-metadata";
import { AlveoFactory, Inject, Injectable, Module } from "../index";
describe("Alveo Modules", () => {
test("should handle Module imports and exports", async () => {
@Injectable()
class SharedService {
identify() {
return "shared";
}
}
@Module({
providers: [SharedService],
exports: [SharedService],
})
class LibModule {}
@Module({
imports: [LibModule],
})
class AppModule {}
const app = await AlveoFactory.create(AppModule);
const service = await app.get(SharedService);
expect(service.identify()).toBe("shared");
});
test("should support Synchronous Dynamic Modules", async () => {
@Injectable()
class DynamicService {
constructor(@Inject("CONFIG") public readonly config: string) {}
}
@Module({})
class DynamicModule {
static forRoot(config: string) {
return {
module: DynamicModule,
providers: [
DynamicService,
{ provide: "CONFIG", useValue: config },
],
exports: [DynamicService],
};
}
}
@Module({
imports: [DynamicModule.forRoot("dynamic_config")],
})
class AppModule {}
const app = await AlveoFactory.create(AppModule);
const service = await app.get(DynamicService);
expect(service.config).toBe("dynamic_config");
});
test("should support Asynchronous Dynamic Modules", async () => {
@Injectable()
class AsyncDynamicService {
constructor(
@Inject("ASYNC_CONFIG") public readonly config: string,
) {}
}
@Module({})
class AsyncDynamicModule {
static async forRoot(config: string) {
await new Promise((resolve) => setTimeout(resolve, 10));
return {
module: AsyncDynamicModule,
providers: [
AsyncDynamicService,
{ provide: "ASYNC_CONFIG", useValue: config },
],
exports: [AsyncDynamicService],
};
}
}
@Module({
imports: [AsyncDynamicModule.forRoot("async_dynamic_config")],
})
class AppModule {}
const app = await AlveoFactory.create(AppModule);
const service = await app.get(AsyncDynamicService);
expect(service.config).toBe("async_dynamic_config");
});
});