From ea16156f6e9f495663f5314dd8beeecd5a273d90 Mon Sep 17 00:00:00 2001 From: Shinwoo PARK Date: Fri, 15 Dec 2023 01:38:24 +0900 Subject: [PATCH] test: add tests for logging return value --- src/test/index.ts | 145 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) diff --git a/src/test/index.ts b/src/test/index.ts index 28430ca..5f1f62d 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -3,6 +3,7 @@ import { ScopedLogger } from "../logger"; import { InjectLogger, LoggedParam, + Returns, ScopeKey, ShouldScoped, } from "../reflected"; @@ -142,6 +143,74 @@ class LoggedClass { ) { logger.log(key); } + + @Returns({ result: "http.result", userId: "body.user.id" }) + async testReturnLogging( + @LoggedParam("userId") + userId: string, + @InjectLogger logger?: ScopedLogger + ) { + logger.log(userId); + return { + http: { + result: "success", + code: 200, + }, + body: { + user: { + id: userId, + name: "tester", + }, + secret: "supersecret", + }, + }; + } + + @Returns({ result: "http.result", userId: "body.user.id" }) + async testMissingReturnLogging( + @LoggedParam("userId") + userId: string, + @InjectLogger logger?: ScopedLogger + ) { + logger.log(userId); + return { + body: { + user: { + id: userId, + name: "tester", + }, + secret: "supersecret", + }, + }; + } + + @Returns() + async testRawObjectReturnLogging( + @LoggedParam("userId") + userId: string, + @InjectLogger logger?: ScopedLogger + ) { + logger.log(userId); + return { + body: { + user: { + id: userId, + name: "tester", + }, + secret: "supersecret", + }, + }; + } + + @Returns() + async testRawValueReturnLogging( + @LoggedParam("userId") + userId: string, + @InjectLogger logger?: ScopedLogger + ) { + logger.log(userId); + return true; + } } class LoggedMethodsClass { @@ -279,6 +348,78 @@ class LoggedMethodsClass { ) { logger.log(key); } + + @LoggedFunction + @Returns({ result: "http.result", userId: "body.user.id" }) + async testReturnLogging( + @LoggedParam("userId") + userId: string, + @InjectLogger logger?: ScopedLogger + ) { + logger.log(userId); + return { + http: { + result: "success", + code: 200, + }, + body: { + user: { + id: userId, + name: "tester", + }, + secret: "supersecret", + }, + }; + } + + @LoggedFunction + @Returns({ result: "http.result", userId: "body.user.id" }) + async testMissingReturnLogging( + @LoggedParam("userId") + userId: string, + @InjectLogger logger?: ScopedLogger + ) { + logger.log(userId); + return { + body: { + user: { + id: userId, + name: "tester", + }, + secret: "supersecret", + }, + }; + } + + @LoggedFunction + @Returns() + async testRawObjectReturnLogging( + @LoggedParam("userId") + userId: string, + @InjectLogger logger?: ScopedLogger + ) { + logger.log(userId); + return { + body: { + user: { + id: userId, + name: "tester", + }, + secret: "supersecret", + }, + }; + } + + @LoggedFunction + @Returns() + async testRawValueReturnLogging( + @LoggedParam("userId") + userId: string, + @InjectLogger logger?: ScopedLogger + ) { + logger.log(userId); + return true; + } } /** @@ -311,6 +452,10 @@ class LoggedMethodsClass { // tester.testOptionalScopedLogging(); // tester.testShouldScopedLogging("asdf") // tester.testShouldScopedLogging(); +// tester.testReturnLogging("asdf"); +// tester.testMissingReturnLogging("asdf"); +// tester.testRawObjectReturnLogging("asdf"); +// tester.testRawValueReturnLogging("asdf"); /** * Then run `yarn test`