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

112
Views
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 answers
Answer question

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 Report

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 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!