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?