diff --git a/backend/src/mailer/sendEMail.test.ts b/backend/src/mailer/sendEMail.test.ts index b7cc06a60..b03a43357 100644 --- a/backend/src/mailer/sendEMail.test.ts +++ b/backend/src/mailer/sendEMail.test.ts @@ -1,13 +1,34 @@ -import { sendEMail } from './sendEMail' +import { sendEMail, logger } from './sendEMail' import { createTransport } from 'nodemailer' import CONFIG from '@/config' +import { getLogger } from '@/server/logger' + CONFIG.EMAIL = false CONFIG.EMAIL_SMTP_URL = 'EMAIL_SMTP_URL' CONFIG.EMAIL_SMTP_PORT = '1234' CONFIG.EMAIL_USERNAME = 'user' CONFIG.EMAIL_PASSWORD = 'pwd' +jest.mock('@/server/logger', () => { + const originalModule = jest.requireActual('@/server/logger') + return { + __esModule: true, + ...originalModule, + getLogger: jest.fn(() => { + return { + addContext: jest.fn(), + trace: jest.fn(), + debug: jest.fn(), + warn: jest.fn(), + info: jest.fn(), + error: jest.fn(), + fatal: jest.fn(), + } + }), + } +}) + jest.mock('nodemailer', () => { return { __esModule: true, @@ -25,12 +46,13 @@ jest.mock('nodemailer', () => { describe('sendEMail', () => { let result: boolean + describe('logger', () => { + it('initializes the logger', () => { + expect(getLogger).toBeCalledWith('backend.mailer.sendEMail') + }) + }) + describe('config email is false', () => { - // eslint-disable-next-line no-console - const consoleLog = console.log - const consoleLogMock = jest.fn() - // eslint-disable-next-line no-console - console.log = consoleLogMock beforeEach(async () => { result = await sendEMail({ to: 'receiver@mail.org', @@ -39,13 +61,8 @@ describe('sendEMail', () => { }) }) - afterAll(() => { - // eslint-disable-next-line no-console - console.log = consoleLog - }) - - it('logs warining to console', () => { - expect(consoleLogMock).toBeCalledWith('Emails are disabled via config') + it('logs warining', () => { + expect(logger.info).toBeCalledWith('Emails are disabled via config...') }) it('returns false', () => { diff --git a/backend/src/mailer/sendEMail.ts b/backend/src/mailer/sendEMail.ts index 192ea6cbf..222813426 100644 --- a/backend/src/mailer/sendEMail.ts +++ b/backend/src/mailer/sendEMail.ts @@ -1,9 +1,9 @@ -import log4js from '@/server/logger' +import { getLogger } from '@/server/logger' import { createTransport } from 'nodemailer' import CONFIG from '@/config' -const logger = log4js.getLogger('backend.mailer.sendEMail') +export const logger = getLogger('backend.mailer.sendEMail') export const sendEMail = async (emailDef: { to: string