72 lines
1.7 KiB
TypeScript
72 lines
1.7 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import "reflect-metadata";
|
|
import { AlveoFactory, Injectable, Module } from "../index";
|
|
|
|
describe("Alveo Lifecycles", () => {
|
|
test("should call lifecycle hooks", async () => {
|
|
let initCalled = false;
|
|
let destroyCalled = false;
|
|
|
|
@Injectable()
|
|
class LifecycleService {
|
|
async onModuleInit() {
|
|
initCalled = true;
|
|
}
|
|
async onModuleDestroy() {
|
|
destroyCalled = true;
|
|
}
|
|
}
|
|
|
|
@Module({
|
|
providers: [LifecycleService],
|
|
})
|
|
class RootModule {}
|
|
|
|
const app = await AlveoFactory.create(RootModule);
|
|
expect(initCalled).toBe(true);
|
|
|
|
await app.close();
|
|
expect(destroyCalled).toBe(true);
|
|
});
|
|
|
|
test("should call all lifecycle hooks in the correct order", async () => {
|
|
const callOrder: string[] = [];
|
|
|
|
@Injectable()
|
|
class FullLifecycleService {
|
|
async onModuleInit() {
|
|
callOrder.push("onModuleInit");
|
|
}
|
|
async onApplicationBootstrap() {
|
|
callOrder.push("onApplicationBootstrap");
|
|
}
|
|
async onModuleDestroy() {
|
|
callOrder.push("onModuleDestroy");
|
|
}
|
|
async beforeApplicationShutdown(signal?: string) {
|
|
callOrder.push(`beforeApplicationShutdown:${signal}`);
|
|
}
|
|
async onApplicationShutdown(signal?: string) {
|
|
callOrder.push(`onApplicationShutdown:${signal}`);
|
|
}
|
|
}
|
|
|
|
@Module({
|
|
providers: [FullLifecycleService],
|
|
})
|
|
class RootModule {}
|
|
|
|
const app = await AlveoFactory.create(RootModule);
|
|
expect(callOrder).toEqual(["onModuleInit", "onApplicationBootstrap"]);
|
|
|
|
await app.close("SIGTERM");
|
|
expect(callOrder).toEqual([
|
|
"onModuleInit",
|
|
"onApplicationBootstrap",
|
|
"onModuleDestroy",
|
|
"beforeApplicationShutdown:SIGTERM",
|
|
"onApplicationShutdown:SIGTERM",
|
|
]);
|
|
});
|
|
});
|