fix test for logging, test logging, silence logger in test

This commit is contained in:
Moriz Wahl 2022-05-10 16:59:09 +02:00
parent 700f8ada07
commit 9e2f04b309
2 changed files with 32 additions and 15 deletions

View File

@ -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', () => {

View File

@ -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