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

159
Views
Imported function not doing what it's supposed to when running jest

DATA STORE FILE:

let data = {
  users: [],
  channels: [],

};
// Use get() to access the data
function getData() {
  return data;
}

// Use set(newData) to pass in the entire data object, with modifications made
function setData(newData) {
  data = newData;
}

export { getData, setData };

clearV1() FILE:

import { getData, setData } from './dataStore';


function clearV1() {

  let data = {
    users: [],
    channels: [],
  };

  setData(data);
  return {};
}

export { clearV1 };

When running the clearV1() function in another, it does not clear the data store. For example: authRegisterV1 creates a user and adds them to the data store channelCreateV1 creates a channel and adds it to the data store

authRegisterV1('test1@gmail.com','test123','Firt','Last');

clearV1()

authRegisterV1('test2@gmail.com','test123','Firt','Last');

expected output: 
{
      users: [
        {
          uId: 1,
          email: 'test2@gmail.com',
          password: 'test123',
          nameFirst: 'Firt',
          nameLast: 'Last',
          handle: 'firtlast0',
          permissionId: 2
        }
      ],
      channels: [],
}

wrong output:
{
      users: [
        {
          uId: 1,
          email: 'test1@gmail.com',
          password: 'test123',
          nameFirst: 'Firt',
          nameLast: 'Last',
          handle: 'firtlast',
          permissionId: 1
        },
        {
          uId: 2,
          email: 'test2@gmail.com',
          password: 'test123',
          nameFirst: 'Firt',
          nameLast: 'Last',
          handle: 'firtlast0',
          permissionId: 2
        }
      ],
      channels: [],
}

I believe the implementation of the clearV1() function is correct, what other possible reason could there be for this error? I imported all the used functions into the test file.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think the problem your facing is the fact that you created data inside of ./dataStore, and thus, it does not exist in clearV1() file. Another way to put is, when you made the data variable initially, it was made in ./dataStore and only exists there. So it makes a new variable instead of updating the existing one.

Another problem is, you are trying to using let data = [value]. let creates the variable just inside of the function you called it in, ignoring any variables on the outside. As a rule of thumb in javascript, when updating an existing variable, use [name] = [value].

If you want to learn more, here's the MDN docs for import statements and let statements.

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!