Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda