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

73
Views
Mocking lambda-log options.meta using jest

In my util.ts I am importing and using lambda-log:

util.ts

import * as logger from 'lambda-log';

export async function prepareMessage(req){
   try {
      logger.options.meta.clientId = req.clientId;
   
      if (!req.templateId) {
         throw new Error(`template is missing`);
      }
   } catch(error){
     logger.error('error occured', error);
   }
}

I want to test in my util.test.ts that logger.error contains clientId I set in options.meta.clientId. This is what I got so far, but it is not working:

util.test.ts

import * as logger from 'lambda-log';
jest.mock('lambda-log', () => ({
    ...jest.requireActual('lambda-log'),
    error: jest.fn()
}));

it('sets clientId in logger.options.meta from request payload', async () => {
   const record = { clientId: 'client-test' };
   const optionsSpy = jest.spyOn(logger, 'error');

   try {
      await prepareMessage(record);
   } catch (err) {
      
      expect(optionsSpy).toHaveBeenCalled();

      expect(optionsSpy).toHaveBeenCalledWith(
      expect.objectContaining({
           clientId: 'client-test'
         })
      );
   }
}

Does anyone know how to spy lambda-log and how to accomplish this?

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!