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

268
Views
Jest variable outside function is undefined while testing function

below is a function i want to test

import { logger } from 'logger';
const log = logger(__filename);

export const foo = () => {
 log('test log');
};

below is my test case

import { foo } from '../../src/utils/foo';
import * as utils from 'logger';
jest.mock('logger');
// eslint-disable-next-line no-undef
const mockedFoo = jest.mocked(utils, true);

describe('Common Utils', function () {
  describe('#foo', function () {
   it('foo', () => {
      // eslint-disable-next-line no-undef
      mockedFoo.logger.mockReturnValueOnce(() => { return { log: jest.fn() }; });
      await foo();
      expect(mockedFoo.logger).toHaveBeenCalledWith('test');
    });
  });
});

when i try to run it gives error log is not defined, it cannot get out of scope variable

about 4 years ago · Juan Pablo Isaza
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!