async-local-storage in nest.js tests

Viewed 504

I've implemented async-local storage in nest.js from async hooks, but when I come to my mocha tests, I need to send an instance of it as a mock and I don't know what's the best way to do that.

I've tried two things: First, to call from my beforeEach function in tests to call my getCtx() which calls the global store and it looks like this:

export function getCtx() {
  const requestContext = getGlobalStore();
  return requestContext.ctx;
}

Second, to change the function to get a parameter and send my context inside like this:

export function getGlobalStore(
  store: AsyncLocalStorage<ICtx> = globalStore,
): ICtx {
  const context = store.getStore();
  return context!;
}

and then in the test :

const store = new  AsyncLocalStorage<CtxMock>();
const context = getGlobalStore(store);

In both ways I get the error: "TypeError: Cannot read properties of undefined (reading 'ctx')"

0 Answers
Related