Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

89
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda