test: add tests for new scoped logger system

This commit is contained in:
p-sw 2024-03-31 19:00:24 +09:00
parent de0cf81f10
commit 60c15330a1

View File

@ -1,4 +1,4 @@
import { LoggedFunction, LoggedInjectable, LoggedRoute } from "../logged"; import { LoggedFunction, LoggedInjectable } from "../logged";
import { ScopedLogger } from "../logger"; import { ScopedLogger } from "../logger";
import { import {
InjectLogger, InjectLogger,
@ -13,15 +13,20 @@ type TestObject = {
e: { p: string; g: number }; e: { p: string; g: number };
}; };
const testObject: TestObject = { @LoggedInjectable()
a: "asdf", class TestService {
b: { c: "zxcv", f: 1 }, public async service(paramA: string, @InjectLogger logger: ScopedLogger) {
d: [2, "qwer"], logger.log(`received paramA ${paramA}`);
e: { p: "uiop", g: 3 }, return paramA
}; }
}
@LoggedInjectable() @LoggedInjectable()
class LoggedClass { class LoggedClass {
constructor(
private service: TestService
) { }
async testParameterLoggingWithoutInjection(@Logged("key") key: number) { async testParameterLoggingWithoutInjection(@Logged("key") key: number) {
console.log(key); console.log(key);
} }
@ -175,9 +180,17 @@ class LoggedClass {
testSyncLogging(@InjectLogger logger?: ScopedLogger) { testSyncLogging(@InjectLogger logger?: ScopedLogger) {
logger.log("synced yay"); logger.log("synced yay");
} }
async testService(@InjectLogger logger?: ScopedLogger) {
this.service.service('A', logger);
}
} }
class LoggedMethodsClass { class LoggedMethodsClass {
constructor(
private service: TestService
) { }
@LoggedFunction @LoggedFunction
async testParameterLoggingWithoutInjection(@Logged("key") key: number) { async testParameterLoggingWithoutInjection(@Logged("key") key: number) {
console.log(key); console.log(key);
@ -349,13 +362,22 @@ class LoggedMethodsClass {
testSyncLogging(@InjectLogger logger?: ScopedLogger) { testSyncLogging(@InjectLogger logger?: ScopedLogger) {
logger.log("synced yay"); logger.log("synced yay");
} }
@LoggedFunction
async testService(@InjectLogger logger?: ScopedLogger) {
this.service.service('A', logger);
}
} }
const service = new TestService();
/** /**
* Choose Class to Test * Choose Class to Test
*/ */
const tester = new LoggedClass(); const tester = new LoggedClass(service);
// const tester = new LoggedMethodsClass(); // const tester = new LoggedMethodsClass(service);
/** /**
* Choose Method to Test * Choose Method to Test
@ -376,6 +398,7 @@ const tester = new LoggedClass();
// void tester.testLoggerRootLogging(); // void tester.testLoggerRootLogging();
// tester.testSyncLoggerRootLogging(); // tester.testSyncLoggerRootLogging();
// tester.testSyncLogging(); // tester.testSyncLogging();
void tester.testService();
/** /**
* Then run `yarn test` * Then run `yarn test`