Passed
Push — main ( a24599...473189 )
by Lorenzo
01:33 queued 37s
created

src/logging/decorators/InjectLogger.spec.ts   A

Complexity

Total Complexity 2
Complexity/F 1

Size

Lines of Code 66
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 49
mnd 0
bc 0
fnc 2
dl 0
loc 66
bpm 0
cpm 1
noi 0
c 0
b 0
f 0
rs 10

2 Functions

Rating   Name   Duplication   Size   Complexity  
A Class.getLogger 0 3 1
A ClassBean.getLogger 0 3 1
1
import { flushPromises } from '@test/utils/testUtils';
2
import { InjectLogger } from '@/logging/decorators/InjectLogger';
3
import { Bean, Logger as PinoLogger } from '@/main';
4
import Logger from '@/logging/Logger';
5
6
jest.mock('@/core', () => ({
7
  registeredBeans: new Map(),
8
  registeredMethods: new Map(),
9
  logger: {
10
    info: jest.fn(),
11
    debug: jest.fn(),
12
    error: jest.fn(),
13
  },
14
}));
15
jest.mock('@/logging/Logger');
16
17
describe('InjectLogger.ts', () => {
18
  beforeEach(() => {
19
    jest.resetAllMocks();
20
    (Logger as jest.Mock).mockImplementation(jest.requireActual('@/logging/Logger').default);
21
  });
22
23
  it('injects a logger', async () => {
24
    // GIVEN
25
    class Class {
26
      @InjectLogger()
27
      private logger!: PinoLogger;
28
29
      getLogger() {
30
        return this.logger;
31
      }
32
    }
33
34
    // WHEN
35
    const instance = new Class();
36
    await flushPromises();
37
38
    // THEN
39
    expect(instance.getLogger()).toBeDefined();
40
  });
41
42
  it('injects a logger with a scope', async () => {
43
    // GIVEN
44
    @Bean
45
    class ClassBean {
46
      @InjectLogger('ClassBean')
47
      private logger!: PinoLogger;
48
49
      getLogger() {
50
        return this.logger;
51
      }
52
    }
53
54
    // WHEN
55
    const instance = new ClassBean();
56
    await flushPromises();
57
    await flushPromises();
58
    await flushPromises();
59
60
    // THEN
61
    instance.getLogger().debug('ciao');
62
    expect(instance.getLogger()).toBeDefined();
63
    expect(Logger).toHaveBeenCalledWith('ClassBean');
64
  });
65
});
66