mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Silence logger for unit tests in user resolver. Make tests available for logger in user resolver
This commit is contained in:
parent
cb83027bef
commit
80b7305860
@ -12,7 +12,28 @@ import { User } from '@entity/User'
|
||||
import CONFIG from '@/config'
|
||||
import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail'
|
||||
import { sendResetPasswordEmail } from '@/mailer/sendResetPasswordEmail'
|
||||
import { printTimeDuration, activationLink } from './UserResolver'
|
||||
import { printTimeDuration, activationLink, logger } from './UserResolver'
|
||||
|
||||
import { getLogger } from '@/server/logger'
|
||||
|
||||
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(),
|
||||
}
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
||||
// import { klicktippSignIn } from '@/apis/KlicktippController'
|
||||
|
||||
@ -56,6 +77,16 @@ afterAll(async () => {
|
||||
})
|
||||
|
||||
describe('UserResolver', () => {
|
||||
describe('logger', () => {
|
||||
it('creates a logger', () => {
|
||||
expect(getLogger).toBeCalledWith('backend.graphql.resolver.UserResolver')
|
||||
})
|
||||
|
||||
it('adds user context to logger', () => {
|
||||
expect(logger.addContext).toBeCalledWith('user', 'unknown')
|
||||
})
|
||||
})
|
||||
|
||||
describe('createUser', () => {
|
||||
const variables = {
|
||||
email: 'peter@lustig.de',
|
||||
@ -149,12 +180,14 @@ describe('UserResolver', () => {
|
||||
})
|
||||
|
||||
describe('email already exists', () => {
|
||||
it('throws an error', async () => {
|
||||
await expect(mutate({ mutation: createUser, variables })).resolves.toEqual(
|
||||
it('throws and logs an error', async () => {
|
||||
const mutation = await mutate({ mutation: createUser, variables })
|
||||
expect(mutation).toEqual(
|
||||
expect.objectContaining({
|
||||
errors: [new GraphQLError('User already exists.')],
|
||||
}),
|
||||
)
|
||||
expect(logger.error).toBeCalledWith('User already exists with this email=peter@lustig.de')
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import fs from 'fs'
|
||||
import log4js from '@/server/logger'
|
||||
import { getLogger } from '@/server/logger'
|
||||
|
||||
import { Context, getUser } from '@/server/context'
|
||||
import { Resolver, Query, Args, Arg, Authorized, Ctx, UseMiddleware, Mutation } from 'type-graphql'
|
||||
@ -28,7 +28,7 @@ const sodium = require('sodium-native')
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const random = require('random-bigint')
|
||||
|
||||
const logger = log4js.getLogger('backend.graphql.resolver.UserResolver')
|
||||
export const logger = getLogger('backend.graphql.resolver.UserResolver')
|
||||
logger.addContext('user', 'unknown')
|
||||
|
||||
// We will reuse this for changePassword
|
||||
|
||||
@ -1,6 +1,10 @@
|
||||
import log4js from 'log4js'
|
||||
import log4js, { Logger } from 'log4js'
|
||||
import CONFIG from '@/config'
|
||||
|
||||
log4js.configure(CONFIG.LOG4JS_CONFIG)
|
||||
|
||||
export const getLogger = (name: string): Logger => {
|
||||
return log4js.getLogger(name)
|
||||
}
|
||||
|
||||
export default log4js
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user