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

82
Vistas
Reset an object import in a beforeEach

I want to remember the technical explanation of this behavior and how to do it properly

sample.js (in reality this is a really long mock which is why its in a separate file)

const data = {
  a = 1,
  nested: {
     object: 'old'
  }
}

export default data;

data.spec.js

import sampleData from './sample'

describe('sample', () => {
let data;
 beforeEach(
  data = sampleData
 )
  it('change data', () => {
    data.nested.object = 'new'; // lets say value was old before
    ....
  })
  it('change data', () => {
   console.log(data.nested.object) // i would think it would be 'old' but is still 'new'
    ....
  })

My brain says that its doing so because all i did was add a pointer to the object and i'm still modifying the object so the beforeEach doesn't do anything. I feel like there's a longer explanation that I'm forgetting

But I want to know the proper way to do it. if I turn the sample data file to a function and have it 'return' the object each time That seems right given I use an arrow function. But that feels incorrect. Is there something I'm forgetting?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Your assumption is correct, just don't forget to make a copy of your test data before changing it.

For example:

beforeEach(() => {
  data = structuredClone(sampleData);
});

Or for older ecmascript version:

beforeEach(() => {
  data = { ...sampleData, nested: { ...sampleData.nested } };
});
about 4 years ago · Santiago Trujillo 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