|
1 | | -import { describe } from '@jest/globals' |
2 | | -import { afterEach } from '@jest/globals' |
3 | | -import { jest } from '@jest/globals' |
4 | | -import { it } from '@jest/globals' |
5 | | -import { expect } from '@jest/globals' |
6 | | - |
7 | | -import { Logger } from './logger' |
8 | | -import { configuration } from './logger.configuration' |
| 1 | +/* eslint-disable */ |
| 2 | +import type { LogRecord } from './logger.interfaces.js' |
| 3 | + |
| 4 | +import { strictEqual } from 'node:assert/strict' |
| 5 | +import { describe as suite } from 'node:test' |
| 6 | +import { beforeEach } from 'node:test' |
| 7 | +import { afterEach } from 'node:test' |
| 8 | +import { test } from 'node:test' |
| 9 | + |
| 10 | +import { Logger } from './logger.js' |
| 11 | +import { configuration } from './logger.configuration.js' |
| 12 | + |
| 13 | +suite('logger', () => { |
| 14 | + let originalTransport: Logger |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + // @ts-expect-error - Transport is a Logger |
| 18 | + originalTransport = configuration.transport |
| 19 | + // @ts-expect-error - Transport is a Logger |
| 20 | + configuration.transport = { |
| 21 | + info: () => {}, |
| 22 | + debug: () => {}, |
| 23 | + warn: () => {}, |
| 24 | + error: () => {}, |
| 25 | + fatal: () => {}, |
| 26 | + trace: () => {}, |
| 27 | + } |
| 28 | + }) |
9 | 29 |
|
10 | | -describe('logger', () => { |
11 | 30 | afterEach(() => { |
12 | | - jest.clearAllMocks() |
| 31 | + // @ts-expect-error - Transport is a Logger |
| 32 | + configuration.transport = originalTransport |
13 | 33 | }) |
14 | 34 |
|
15 | | - it('log body', () => { |
16 | | - const log = jest.spyOn(configuration.transport, 'info').mockImplementation(() => undefined) |
| 35 | + test('log body', () => { |
| 36 | + let calledWith |
| 37 | + // @ts-expect-error - mock LogFn |
| 38 | + configuration.transport.info = (record: LogRecord) => { |
| 39 | + calledWith = record |
| 40 | + } |
17 | 41 |
|
18 | 42 | new Logger().info('test') |
19 | 43 |
|
20 | | - expect(log).toHaveBeenCalledWith(expect.objectContaining({ body: 'test' })) |
| 44 | + // @ts-expect-error - body is a string |
| 45 | + strictEqual(calledWith.body, 'test') |
21 | 46 | }) |
22 | 47 |
|
23 | | - it('log attributes', () => { |
24 | | - const log = jest.spyOn(configuration.transport, 'info').mockImplementation(() => undefined) |
| 48 | + test('log attributes', () => { |
| 49 | + let calledWith |
| 50 | + // @ts-expect-error - mock LogFn |
| 51 | + configuration.transport.info = (record: LogRecord) => { |
| 52 | + calledWith = record |
| 53 | + } |
25 | 54 |
|
26 | 55 | new Logger().info('test', { attr: 'test' }) |
27 | 56 |
|
28 | | - expect(log).toHaveBeenCalledWith( |
29 | | - expect.objectContaining({ body: 'test', attributes: { attr: 'test' } }) |
30 | | - ) |
| 57 | + // @ts-expect-error - attributes |
| 58 | + strictEqual(calledWith.attributes.attr, 'test') |
31 | 59 | }) |
32 | 60 |
|
33 | | - it('log name', () => { |
34 | | - const log = jest.spyOn(configuration.transport, 'info').mockImplementation(() => undefined) |
| 61 | + test('log name', () => { |
| 62 | + let calledWith |
| 63 | + // @ts-expect-error - mock LogFn |
| 64 | + configuration.transport.info = (record) => { |
| 65 | + calledWith = record |
| 66 | + } |
35 | 67 |
|
36 | 68 | new Logger('test').info('test') |
37 | 69 |
|
38 | | - expect(log).toHaveBeenCalledWith(expect.objectContaining({ name: 'test' })) |
| 70 | + // @ts-expect-error - name |
| 71 | + strictEqual(calledWith.name, 'test') |
39 | 72 | }) |
40 | 73 |
|
41 | | - it('log child name', () => { |
42 | | - const log = jest.spyOn(configuration.transport, 'info').mockImplementation(() => undefined) |
| 74 | + test('log child name', () => { |
| 75 | + let calledWith |
| 76 | + // @ts-expect-error - mock LogFn |
| 77 | + configuration.transport.info = (record) => { |
| 78 | + calledWith = record |
| 79 | + } |
43 | 80 |
|
44 | 81 | new Logger('parent').child('child').info('test') |
45 | 82 |
|
46 | | - expect(log).toHaveBeenCalledWith(expect.objectContaining({ name: 'parent:child' })) |
| 83 | + // @ts-expect-error - name |
| 84 | + strictEqual(calledWith.name, 'parent:child') |
47 | 85 | }) |
48 | 86 |
|
49 | | - it('log child attributes', () => { |
50 | | - const log = jest.spyOn(configuration.transport, 'info').mockImplementation(() => undefined) |
| 87 | + test('log child attributes', () => { |
| 88 | + let calledWith |
| 89 | + // @ts-expect-error - mock LogFn |
| 90 | + configuration.transport.info = (record) => { |
| 91 | + calledWith = record |
| 92 | + } |
51 | 93 |
|
52 | 94 | new Logger('parent', { parent: true }).child('child', { child: true }).info('test') |
53 | 95 |
|
54 | | - expect(log).toHaveBeenCalledWith( |
55 | | - expect.objectContaining({ attributes: { parent: true, child: true } }) |
56 | | - ) |
| 96 | + // @ts-expect-error - attributes |
| 97 | + strictEqual(calledWith.attributes.parent, true) |
| 98 | + // @ts-expect-error - attributes |
| 99 | + strictEqual(calledWith.attributes.child, true) |
57 | 100 | }) |
58 | 101 |
|
59 | | - it('log debug enabled', () => { |
| 102 | + test('log debug enabled', () => { |
60 | 103 | configuration.setDebug('debug') |
61 | 104 |
|
62 | | - const log = jest.spyOn(configuration.transport, 'debug').mockImplementation(() => undefined) |
| 105 | + let calledWith |
| 106 | + // @ts-expect-error - mock LogFn |
| 107 | + configuration.transport.debug = (record) => { |
| 108 | + calledWith = record |
| 109 | + } |
63 | 110 |
|
64 | 111 | new Logger('debug').debug('debug') |
65 | 112 |
|
66 | | - expect(log).toHaveBeenCalledWith(expect.objectContaining({ body: 'debug' })) |
| 113 | + // @ts-expect-error - body |
| 114 | + strictEqual(calledWith.body, 'debug') |
67 | 115 | }) |
68 | 116 | }) |
0 commit comments