Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
async-local-storage in nest.js tests

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')"

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!