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

120
Vistas
Confusion about scope in Jest tests

Can someone explain simply what the practical difference is in how each of the following tests is executed:

Case 1

const variable = generateVariable();

test('generates the correct value', () => {
   expect(variable).toBe('desired-value');
});

Case 2

test('generates the correct value', () => {
   const variable = generateVariable();
   expect(variable).toBe('desired-value');
});

Case 3

describe('Variable generator', () => {
   beforeEach(() => {
     const variable = generateVariable();
   });
   test('generates the correct value', () => {
      expect(variable).toBe('desired-value');
   });
});

Case 4

describe('Variable generator', () => {
   beforeAll(() => {
     const variable = generateVariable();
   });
   test('generates the correct value', () => {
      expect(variable).toBe('desired-value');
   });
});
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Case 1

  1. const variable = generateVariable();
  2. jest machine will read the tests, and execute them

Case 2

  1. Jest machine will read tests and execute them
  2. Which will do the vadiable and expect.

Case 3

  1. Jest machine will read the tests and execute them
  2. Just before each "test" jest will execute the function beforeEach (creating const variable in scope of beforeEach function). (this would be better visible with more than one test)
  3. Will execute the tests and seeing unknown identifier "variable" will tell you about error"

Case 4

  1. Jest machine will read the tests and execute them
  2. Just before 1st test, jest will execute the function beforeAll (creating const variable in scope of this function). (this would be better visible with more than one test)
  3. Will execute the tests and seeing unknown identifier "variable" will tell you about error"
about 4 years ago · Juan Pablo Isaza Denunciar

0

In case 1, variable will be available to all tests. Ideally, this should be something that it's not mutated in tests, otherwise tests might randomly fail.

Case 2 is the best way if you want to keep variable in isolation. Its binding is only valid within the test body. Other occurrences of variable can be declared in other tests.

In case 3 and 4, you won't be able to use variable in the test, as it's declared and initialized within a different function body (beforeEach or beforeAll). As Seti mentioned in the previous answer, you'll get an error when trying to use variable.

There's a subtle difference to overcome this issue:

let variable;
describe('Variable generator', () => {
   beforeEach(() => {
     variable = generateVariable();
   });
   test('generates the correct value', () => {
      expect(variable).toBe('desired-value');
   });
});

In this case, variable can be used in all the tests, and it will be initialized before each test. It's a common practice if you want to initialize something the same way in many tests.

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