import {Inject, Injectable, PlatformContext, PlatformHandler, PlatformTest} from "@tsed/common";
import {Logger} from "@tsed/logger";
import {AsyncLocalStorage} from "async_hooks";

let store: AsyncLocalStorage<PlatformContext>;

@Injectable()
export class PlatformAsyncHookContext {
  @Inject()
  protected logger: Logger;

  @Inject()
  protected platformHandler: PlatformHandler;

  getContext() {
    return store?.getStore();
  }

  static getStore() {
    store = store || new AsyncLocalStorage();
    return store;
  }

  run = (ctx: PlatformContext, cb: any) => {
    return PlatformAsyncHookContext.run(ctx, cb);
  };

  $onInit() {
    /* istanbul ignore */
    if (AsyncLocalStorage) {
      PlatformAsyncHookContext.getStore();
      // override
      this.platformHandler.run = this.run;
    } else {
      this.logger.warn(
        `AsyncLocalStorage is not available for your Node.js version (${process.versions.node}). Please upgrade your version at least to v13.10.0.`
      );
    }
  }

  static run(ctx: PlatformContext, cb: any) {
    return PlatformAsyncHookContext.getStore().run(ctx, cb);
  }
}

import {Inject, Injectable, PlatformContext, PlatformHandler} from "@tsed/common";
import {Logger} from "@tsed/logger";
import {AsyncLocalStorage} from "async_hooks";

@Injectable()
export class PlatformAsyncHookContext {
  @Inject()
  protected logger: Logger;

  @Inject()
  protected platformHandler: PlatformHandler;

  protected store: AsyncLocalStorage<PlatformContext>;

  getContext() {
    return this.store?.getStore();
  }

  $onInit() {
    /* istanbul ignore */
    if (AsyncLocalStorage) {
      this.store = new AsyncLocalStorage();
      // override
      this.platformHandler.run = (ctx: PlatformContext, cb: any) => {
        return PlatformAsyncHookContext.run(ctx, cb, this.store);
      };
    } else {
      this.logger.warn(
        `AsyncLocalStorage is not available for your Node.js version (${process.versions.node}). Please upgrade your version at least to v13.10.0.`
      );
    }
  }

  static run(ctx: PlatformContext, cb: any, store = new AsyncLocalStorage()) {
    return store.run(ctx, cb);
  }
}

--- 
+++ 
@@ -1,6 +1,8 @@
-import {Inject, Injectable, PlatformContext, PlatformHandler} from "@tsed/common";
+import {Inject, Injectable, PlatformContext, PlatformHandler, PlatformTest} from "@tsed/common";
 import {Logger} from "@tsed/logger";
 import {AsyncLocalStorage} from "async_hooks";
+
+let store: AsyncLocalStorage<PlatformContext>;
 
 @Injectable()
 export class PlatformAsyncHookContext {
@@ -10,20 +12,25 @@
   @Inject()
   protected platformHandler: PlatformHandler;
 
-  protected store: AsyncLocalStorage<PlatformContext>;
+  getContext() {
+    return store?.getStore();
+  }
 
-  getContext() {
-    return this.store?.getStore();
+  static getStore() {
+    store = store || new AsyncLocalStorage();
+    return store;
   }
+
+  run = (ctx: PlatformContext, cb: any) => {
+    return PlatformAsyncHookContext.run(ctx, cb);
+  };
 
   $onInit() {
     /* istanbul ignore */
     if (AsyncLocalStorage) {
-      this.store = new AsyncLocalStorage();
+      PlatformAsyncHookContext.getStore();
       // override
-      this.platformHandler.run = (ctx: PlatformContext, cb: any) => {
-        return PlatformAsyncHookContext.run(ctx, cb, this.store);
-      };
+      this.platformHandler.run = this.run;
     } else {
       this.logger.warn(
         `AsyncLocalStorage is not available for your Node.js version (${process.versions.node}). Please upgrade your version at least to v13.10.0.`
@@ -31,7 +38,7 @@
     }
   }
 
-  static run(ctx: PlatformContext, cb: any, store = new AsyncLocalStorage()) {
-    return store.run(ctx, cb);
+  static run(ctx: PlatformContext, cb: any) {
+    return PlatformAsyncHookContext.getStore().run(ctx, cb);
   }
 }
fix(async-hook-context): Fix asyncHookContext when running unit test
