-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: only create one log file and only as needed * test: add regression test for multiple logs
- Loading branch information
1 parent
ef532b9
commit cb78770
Showing
3 changed files
with
122 additions
and
15 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
packages/core/strapi/src/cli/utils/__tests__/logger.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { type Logger, createLogger } from '@strapi/logger'; | ||
import { formatDiagnostic } from '../data-transfer'; | ||
|
||
jest.mock('@strapi/logger', () => { | ||
const actualWinston = jest.requireActual('winston'); | ||
return { | ||
...actualWinston, | ||
createLogger: jest.fn(), | ||
configs: { | ||
createOutputFileConfiguration: jest.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
// Mock console methods to avoid cluttering test output | ||
const consoleMock = { | ||
error: jest.spyOn(console, 'error').mockImplementation(() => {}), | ||
warn: jest.spyOn(console, 'warn').mockImplementation(() => {}), | ||
info: jest.spyOn(console, 'info').mockImplementation(() => {}), | ||
}; | ||
|
||
describe('logger', () => { | ||
let mockLogger: jest.Mocked<Logger>; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
// Create a mock logger with jest.fn() methods | ||
mockLogger = { | ||
error: jest.fn(), | ||
warn: jest.fn(), | ||
info: jest.fn(), | ||
debug: jest.fn(), | ||
} as any; | ||
|
||
// Mock createLogger to return the mock logger | ||
(createLogger as jest.Mock).mockReturnValue(mockLogger); | ||
}); | ||
|
||
afterEach(() => { | ||
// Clear console mocks after each test | ||
consoleMock.error.mockClear(); | ||
consoleMock.warn.mockClear(); | ||
consoleMock.info.mockClear(); | ||
}); | ||
|
||
describe('formatDiagnostic', () => { | ||
it('only creates a single logger', () => { | ||
const diagnosticReporter = formatDiagnostic('export'); | ||
|
||
// Use the diagnostic reporter to log different levels of messages | ||
diagnosticReporter({ | ||
kind: 'error', | ||
details: { | ||
message: 'Test error', | ||
createdAt: new Date(), | ||
name: '', | ||
severity: 'error', | ||
error: new Error(), | ||
}, | ||
}); | ||
diagnosticReporter({ | ||
kind: 'warning', | ||
details: { | ||
message: 'Test warning', | ||
createdAt: new Date(), | ||
}, | ||
}); | ||
diagnosticReporter({ | ||
kind: 'info', | ||
details: { | ||
message: 'Test info', | ||
createdAt: new Date(), | ||
}, | ||
}); | ||
|
||
// Verify createLogger is called only once | ||
expect(createLogger).toHaveBeenCalledTimes(1); | ||
|
||
// Verify the mock logger received the logs | ||
expect(mockLogger.error).toHaveBeenCalledTimes(1); | ||
expect(mockLogger.error).toHaveBeenCalledWith(expect.stringContaining('Test error')); | ||
expect(mockLogger.warn).toHaveBeenCalledTimes(1); | ||
expect(mockLogger.warn).toHaveBeenCalledWith(expect.stringContaining('Test warning')); | ||
expect(mockLogger.info).toHaveBeenCalledTimes(1); | ||
expect(mockLogger.info).toHaveBeenCalledWith(expect.stringContaining('Test info')); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters