|
1 | 1 | import { expect } from 'chai'; |
2 | | -import 'reflect-metadata'; |
3 | 2 | import * as sinon from 'sinon'; |
4 | 3 | import { ConsoleLogger, Logger, LoggerService, LogLevel } from '../../services'; |
5 | 4 |
|
@@ -475,6 +474,85 @@ describe('Logger', () => { |
475 | 474 | expect(processStdoutWriteSpy.secondCall.firstArg).to.include(Test.name); |
476 | 475 | }); |
477 | 476 | }); |
| 477 | + |
| 478 | + describe('forceConsole option', () => { |
| 479 | + let consoleLogSpy: sinon.SinonSpy; |
| 480 | + let consoleErrorSpy: sinon.SinonSpy; |
| 481 | + let processStdoutWriteStub: sinon.SinonStub; |
| 482 | + let processStderrWriteStub: sinon.SinonStub; |
| 483 | + |
| 484 | + beforeEach(() => { |
| 485 | + // Stub process.stdout.write to prevent actual output and track calls |
| 486 | + processStdoutWriteStub = sinon.stub(process.stdout, 'write'); |
| 487 | + processStderrWriteStub = sinon.stub(process.stderr, 'write'); |
| 488 | + consoleLogSpy = sinon.spy(console, 'log'); |
| 489 | + consoleErrorSpy = sinon.spy(console, 'error'); |
| 490 | + }); |
| 491 | + |
| 492 | + afterEach(() => { |
| 493 | + processStdoutWriteStub.restore(); |
| 494 | + processStderrWriteStub.restore(); |
| 495 | + consoleLogSpy.restore(); |
| 496 | + consoleErrorSpy.restore(); |
| 497 | + }); |
| 498 | + |
| 499 | + it('should use console.log instead of process.stdout.write when forceConsole is true', () => { |
| 500 | + const logger = new ConsoleLogger({ forceConsole: true }); |
| 501 | + const message = 'test message'; |
| 502 | + |
| 503 | + logger.log(message); |
| 504 | + |
| 505 | + // When forceConsole is true, console.log should be called |
| 506 | + expect(consoleLogSpy.called).to.be.true; |
| 507 | + expect(consoleLogSpy.firstCall.firstArg).to.include(message); |
| 508 | + }); |
| 509 | + |
| 510 | + it('should use console.error instead of process.stderr.write when forceConsole is true', () => { |
| 511 | + const logger = new ConsoleLogger({ forceConsole: true }); |
| 512 | + const message = 'error message'; |
| 513 | + |
| 514 | + logger.error(message); |
| 515 | + |
| 516 | + expect(consoleErrorSpy.called).to.be.true; |
| 517 | + expect(consoleErrorSpy.firstCall.firstArg).to.include(message); |
| 518 | + }); |
| 519 | + |
| 520 | + it('should use console.error for stack traces when forceConsole is true', () => { |
| 521 | + const logger = new ConsoleLogger({ forceConsole: true }); |
| 522 | + const message = 'error with stack'; |
| 523 | + const stack = 'Error: test\n at <anonymous>:1:1'; |
| 524 | + |
| 525 | + logger.error(message, stack); |
| 526 | + |
| 527 | + expect(consoleErrorSpy.calledTwice).to.be.true; |
| 528 | + expect(consoleErrorSpy.firstCall.firstArg).to.include(message); |
| 529 | + expect(consoleErrorSpy.secondCall.firstArg).to.equal(stack); |
| 530 | + }); |
| 531 | + |
| 532 | + it('should use process.stdout.write when forceConsole is false', () => { |
| 533 | + const logger = new ConsoleLogger({ forceConsole: false }); |
| 534 | + const message = 'test message'; |
| 535 | + |
| 536 | + logger.log(message); |
| 537 | + |
| 538 | + expect(processStdoutWriteStub.called).to.be.true; |
| 539 | + expect(processStdoutWriteStub.firstCall.firstArg).to.include(message); |
| 540 | + expect(consoleLogSpy.called).to.be.false; |
| 541 | + }); |
| 542 | + |
| 543 | + it('should work with JSON mode and forceConsole', () => { |
| 544 | + const logger = new ConsoleLogger({ json: true, forceConsole: true }); |
| 545 | + const message = 'json message'; |
| 546 | + |
| 547 | + logger.log(message); |
| 548 | + |
| 549 | + expect(consoleLogSpy.called).to.be.true; |
| 550 | + |
| 551 | + const output = consoleLogSpy.firstCall.firstArg; |
| 552 | + const json = JSON.parse(output); |
| 553 | + expect(json.message).to.equal(message); |
| 554 | + }); |
| 555 | + }); |
478 | 556 | }); |
479 | 557 |
|
480 | 558 | describe('[instance methods]', () => { |
|
0 commit comments