Skip to content

Commit 9dac561

Browse files
committed
add test
1 parent 0744a1d commit 9dac561

File tree

1 file changed

+86
-17
lines changed

1 file changed

+86
-17
lines changed

packages/@n8n/backend-common/src/logging/__tests__/logger.test.ts

Lines changed: 86 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ jest.mock('n8n-workflow', () => ({
44
}));
55

66
import type { GlobalConfig, InstanceSettingsConfig } from '@n8n/config';
7-
import { mock } from 'jest-mock-extended';
7+
import { mock, captor } from 'jest-mock-extended';
88
import { LoggerProxy } from 'n8n-workflow';
9+
import winston from 'winston';
910

1011
import { Logger } from '../logger';
1112

@@ -37,6 +38,10 @@ describe('Logger', () => {
3738
});
3839

3940
describe('transports', () => {
41+
afterEach(() => {
42+
jest.restoreAllMocks();
43+
});
44+
4045
test('if `console` selected, should set console transport', () => {
4146
const globalConfig = mock<GlobalConfig>({
4247
logging: {
@@ -57,29 +62,93 @@ describe('Logger', () => {
5762
expect(transport.constructor.name).toBe('Console');
5863
});
5964

60-
test('if `file` selected, should set file transport', () => {
61-
const globalConfig = mock<GlobalConfig>({
62-
logging: {
63-
level: 'info',
64-
outputs: ['file'],
65-
scopes: [],
66-
file: {
67-
fileSizeMax: 100,
68-
fileCountMax: 16,
69-
location: 'logs/n8n.log',
65+
describe('`file`', () => {
66+
test('should set file transport', () => {
67+
const globalConfig = mock<GlobalConfig>({
68+
logging: {
69+
level: 'info',
70+
outputs: ['file'],
71+
scopes: [],
72+
file: {
73+
fileSizeMax: 100,
74+
fileCountMax: 16,
75+
location: 'logs/n8n.log',
76+
},
7077
},
71-
},
78+
});
79+
80+
const logger = new Logger(
81+
globalConfig,
82+
mock<InstanceSettingsConfig>({ n8nFolder: '/tmp' }),
83+
);
84+
85+
const { transports } = logger.getInternalLogger();
86+
87+
expect(transports).toHaveLength(1);
88+
89+
const [transport] = transports;
90+
91+
expect(transport.constructor.name).toBe('File');
7292
});
7393

74-
const logger = new Logger(globalConfig, mock<InstanceSettingsConfig>({ n8nFolder: '/tmp' }));
94+
test('should accept absolute paths', () => {
95+
// ARRANGE
96+
const location = '/tmp/n8n.log';
97+
const globalConfig = mock<GlobalConfig>({
98+
logging: {
99+
level: 'info',
100+
outputs: ['file'],
101+
scopes: [],
102+
file: { fileSizeMax: 100, fileCountMax: 16, location },
103+
},
104+
});
105+
const OriginalFile = winston.transports.File;
106+
const FileSpy = jest.spyOn(winston.transports, 'File').mockImplementation((...args) => {
107+
return new OriginalFile(...args);
108+
});
75109

76-
const { transports } = logger.getInternalLogger();
110+
// ACT
111+
new Logger(globalConfig, mock<InstanceSettingsConfig>({ n8nFolder: '/tmp' }));
77112

78-
expect(transports).toHaveLength(1);
113+
// ASSERT
114+
const fileOptionsCaptor = captor<string>();
79115

80-
const [transport] = transports;
116+
expect(FileSpy).toHaveBeenCalledTimes(1);
117+
expect(FileSpy).toHaveBeenCalledWith(fileOptionsCaptor);
118+
expect(fileOptionsCaptor.value).toMatchObject({ filename: location });
119+
});
81120

82-
expect(transport.constructor.name).toBe('File');
121+
test('should accept relative paths', () => {
122+
// ARRANGE
123+
const location = 'tmp/n8n.log';
124+
const n8nFolder = '/tmp/n8n';
125+
const globalConfig = mock<GlobalConfig>({
126+
logging: {
127+
level: 'info',
128+
outputs: ['file'],
129+
scopes: [],
130+
file: {
131+
fileSizeMax: 100,
132+
fileCountMax: 16,
133+
location,
134+
},
135+
},
136+
});
137+
const OriginalFile = winston.transports.File;
138+
const FileSpy = jest.spyOn(winston.transports, 'File').mockImplementation((...args) => {
139+
return new OriginalFile(...args);
140+
});
141+
142+
// ACT
143+
new Logger(globalConfig, mock<InstanceSettingsConfig>({ n8nFolder }));
144+
145+
// ASSERT
146+
const fileOptionsCaptor = captor<string>();
147+
148+
expect(FileSpy).toHaveBeenCalledTimes(1);
149+
expect(FileSpy).toHaveBeenCalledWith(fileOptionsCaptor);
150+
expect(fileOptionsCaptor.value).toMatchObject({ filename: `${n8nFolder}/${location}` });
151+
});
83152
});
84153
});
85154

0 commit comments

Comments
 (0)