Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda