Skip to content

Commit 306aaea

Browse files
committed
fix(logger): use native node test
1 parent 3072613 commit 306aaea

File tree

1 file changed

+80
-32
lines changed

1 file changed

+80
-32
lines changed

packages/logger/src/logger.test.ts

Lines changed: 80 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,116 @@
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+
})
929

10-
describe('logger', () => {
1130
afterEach(() => {
12-
jest.clearAllMocks()
31+
// @ts-expect-error - Transport is a Logger
32+
configuration.transport = originalTransport
1333
})
1434

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+
}
1741

1842
new Logger().info('test')
1943

20-
expect(log).toHaveBeenCalledWith(expect.objectContaining({ body: 'test' }))
44+
// @ts-expect-error - body is a string
45+
strictEqual(calledWith.body, 'test')
2146
})
2247

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+
}
2554

2655
new Logger().info('test', { attr: 'test' })
2756

28-
expect(log).toHaveBeenCalledWith(
29-
expect.objectContaining({ body: 'test', attributes: { attr: 'test' } })
30-
)
57+
// @ts-expect-error - attributes
58+
strictEqual(calledWith.attributes.attr, 'test')
3159
})
3260

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+
}
3567

3668
new Logger('test').info('test')
3769

38-
expect(log).toHaveBeenCalledWith(expect.objectContaining({ name: 'test' }))
70+
// @ts-expect-error - name
71+
strictEqual(calledWith.name, 'test')
3972
})
4073

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+
}
4380

4481
new Logger('parent').child('child').info('test')
4582

46-
expect(log).toHaveBeenCalledWith(expect.objectContaining({ name: 'parent:child' }))
83+
// @ts-expect-error - name
84+
strictEqual(calledWith.name, 'parent:child')
4785
})
4886

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+
}
5193

5294
new Logger('parent', { parent: true }).child('child', { child: true }).info('test')
5395

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)
57100
})
58101

59-
it('log debug enabled', () => {
102+
test('log debug enabled', () => {
60103
configuration.setDebug('debug')
61104

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+
}
63110

64111
new Logger('debug').debug('debug')
65112

66-
expect(log).toHaveBeenCalledWith(expect.objectContaining({ body: 'debug' }))
113+
// @ts-expect-error - body
114+
strictEqual(calledWith.body, 'debug')
67115
})
68116
})

0 commit comments

Comments
 (0)