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

178
Visualizações
In jest I can't access to my exported module

For example, I can't access to this module, why ?

let nodeCacheClient;

module.exports = {
    initNodeCache: () => {
        const NodeCache = require("node-cache");
        nodeCacheClient = new NodeCache();
        return nodeCacheClient;
    },
    insertToCacheWithTtl: (key, obj, ttl) => {
        return nodeCacheClient.set(key, obj, ttl);
    },
    getCache: (key) => {
        return nodeCacheClient.get(key);
    },
    deleteKey: (key) => {
        return nodeCacheClient.del(key);
    },
};

when I run this test I get this : TypeError: Cannot read property 'get' of undefined Error

test("login a user", async () => {
        try {
            const response = await axiosInstance.post("users/login", {
                email: "test@gmail.com",
                password: "144847120",
                otpCode: getCacheClient.getCache("test@gmail.com")
            });
            console.log(response.data);
            expect(response.data.status).toBe("success");
        } catch (error) {
            console.log(error + " Error");
            expect(error);
        }
 });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It’s totally normal!

Actually you got access to your module, and the error is coming into your module where the “nodeCacheClient” is undefined since it was not defined!

Your error is coming from your “getCache()” function, in this syntax:

return nodeCacheClient.get(key);

Where in your test you didnt call for the “initNodeCache()” method which will do

nodeCacheClient = new NodeCache();

So, for your test scope, the nodeCacheClient is undefined, and that’s why

nodeCacheClient.get(key);

Will return the

typeError: Cannot read property 'get' of undefined Error
about 4 years ago · Juan Pablo Isaza Relatório
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