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

446
Views
While testing in javascript, Variables defined in 'before' block is showing undefined while using in 'It' block

This is a sample test where I am testing that testNum should be 0,

code:

describe("Test Contract", () => {
    
    before(async () => {
        const testNum = 0;
    })
    
    it("should be zero", function () {
        expect(testNum).to.equal(0);
    })
  }

But I am getting an error saying testNum is undefined.

Error:

1) Test Contract
       should be zero:
     ReferenceError: testNum is not defined

What I am doing wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Variable testNum is scoped to the {} where you defined it. Also, const is used for read only values. I assume you want to reassign testNum multiple times. Therefore you should use let.

What you probably want is:

describe("Test Contract", () => {
    let testNum;
    before(async () => {
        testNum = 0;
    })
    
    it("should be zero", function () {
        expect(testNum).to.equal(0);
    })
  }
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!