Por ejemplo, no puedo acceder a este módulo, ¿por qué?
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); }, };cuando ejecuto esta prueba obtengo esto: TypeError: No se puede leer la propiedad 'obtener' de un error indefinido
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); } });¡Es totalmente normal!
En realidad, obtuvo acceso a su módulo, y el error está entrando en su módulo donde el " nodeCacheClient " no está definido ya que no estaba definido.
Su error proviene de su función " getCache() ", en esta sintaxis:
return nodeCacheClient.get(key);¿En qué parte de su prueba no solicitó el método “ initNodeCache() ” que servirá?
nodeCacheClient = new NodeCache();Entonces, para su alcance de prueba, nodeCacheClient no está definido , y es por eso que
nodeCacheClient.get(key);devolverá el
typeError: Cannot read property 'get' of undefined Error