test: split container.test.ts into modular test files
This commit is contained in:
71
test/lifecycle.test.ts
Normal file
71
test/lifecycle.test.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
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",
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user