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

182
Views
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 answers
Answer question

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 Report
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!