|
| 1 | +import { ConsoleLogger, INestApplication } from '@nestjs/common'; |
| 2 | +import { Test } from '@nestjs/testing'; |
| 3 | +import * as request from 'supertest'; |
| 4 | +import { AppModule } from '../src/app.module'; |
| 5 | +import * as sinon from 'sinon'; |
| 6 | +import { expect } from 'chai'; |
| 7 | + |
| 8 | +describe('ForceConsole Option', () => { |
| 9 | + let app: INestApplication; |
| 10 | + |
| 11 | + describe('When forceConsole is true', () => { |
| 12 | + let consoleLogSpy: sinon.SinonSpy; |
| 13 | + let consoleErrorSpy: sinon.SinonSpy; |
| 14 | + let processStdoutSpy: sinon.SinonSpy; |
| 15 | + let processStderrSpy: sinon.SinonSpy; |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + // Spy on console and process methods |
| 19 | + consoleLogSpy = sinon.spy(console, 'log'); |
| 20 | + consoleErrorSpy = sinon.spy(console, 'error'); |
| 21 | + processStdoutSpy = sinon.spy(process.stdout, 'write'); |
| 22 | + processStderrSpy = sinon.spy(process.stderr, 'write'); |
| 23 | + |
| 24 | + const moduleRef = await Test.createTestingModule({ |
| 25 | + imports: [AppModule], |
| 26 | + }).compile(); |
| 27 | + |
| 28 | + app = moduleRef.createNestApplication({ |
| 29 | + forceConsole: true, |
| 30 | + logger: ['log', 'error'], |
| 31 | + }); |
| 32 | + |
| 33 | + await app.init(); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(async () => { |
| 37 | + consoleLogSpy.restore(); |
| 38 | + consoleErrorSpy.restore(); |
| 39 | + processStdoutSpy.restore(); |
| 40 | + processStderrSpy.restore(); |
| 41 | + await app.close(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should use console.log instead of process.stdout.write', async () => { |
| 45 | + const logger = new ConsoleLogger('TestContext', { forceConsole: true }); |
| 46 | + logger.log('Test log message'); |
| 47 | + |
| 48 | + // Should use console.log when forceConsole is true |
| 49 | + expect(consoleLogSpy.called).to.be.true; |
| 50 | + // Verify console.log was called with the message |
| 51 | + const consoleLogCalls = consoleLogSpy |
| 52 | + .getCalls() |
| 53 | + .filter(call => |
| 54 | + call.args.some(arg => String(arg).includes('Test log message')), |
| 55 | + ); |
| 56 | + expect(consoleLogCalls.length).to.be.greaterThan(0); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should use console.error instead of process.stderr.write', async () => { |
| 60 | + const logger = new ConsoleLogger('TestContext', { forceConsole: true }); |
| 61 | + logger.error('Test error message'); |
| 62 | + |
| 63 | + // Should use console.error when forceConsole is true |
| 64 | + expect(consoleErrorSpy.called).to.be.true; |
| 65 | + // Verify console.error was called with the message |
| 66 | + const consoleErrorCalls = consoleErrorSpy |
| 67 | + .getCalls() |
| 68 | + .filter(call => |
| 69 | + call.args.some(arg => String(arg).includes('Test error message')), |
| 70 | + ); |
| 71 | + expect(consoleErrorCalls.length).to.be.greaterThan(0); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should handle GET request with forceConsole option enabled', () => { |
| 75 | + return request(app.getHttpServer()).get('/hello').expect(200); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('When forceConsole is false (default)', () => { |
| 80 | + let consoleLogSpy: sinon.SinonSpy; |
| 81 | + let consoleErrorSpy: sinon.SinonSpy; |
| 82 | + let processStdoutSpy: sinon.SinonSpy; |
| 83 | + let processStderrSpy: sinon.SinonSpy; |
| 84 | + |
| 85 | + beforeEach(async () => { |
| 86 | + // Spy on console and process methods |
| 87 | + consoleLogSpy = sinon.spy(console, 'log'); |
| 88 | + consoleErrorSpy = sinon.spy(console, 'error'); |
| 89 | + processStdoutSpy = sinon.spy(process.stdout, 'write'); |
| 90 | + processStderrSpy = sinon.spy(process.stderr, 'write'); |
| 91 | + |
| 92 | + const moduleRef = await Test.createTestingModule({ |
| 93 | + imports: [AppModule], |
| 94 | + }).compile(); |
| 95 | + |
| 96 | + app = moduleRef.createNestApplication({ |
| 97 | + logger: ['log', 'error'], |
| 98 | + // forceConsole is not set, defaults to false |
| 99 | + }); |
| 100 | + |
| 101 | + await app.init(); |
| 102 | + }); |
| 103 | + |
| 104 | + afterEach(async () => { |
| 105 | + consoleLogSpy.restore(); |
| 106 | + consoleErrorSpy.restore(); |
| 107 | + processStdoutSpy.restore(); |
| 108 | + processStderrSpy.restore(); |
| 109 | + await app.close(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should not directly call console.log when forceConsole is false', async () => { |
| 113 | + const logger = new ConsoleLogger('TestContext'); |
| 114 | + |
| 115 | + // Reset spy to ensure clean state |
| 116 | + consoleLogSpy.resetHistory(); |
| 117 | + |
| 118 | + logger.log('Test log message'); |
| 119 | + |
| 120 | + // When forceConsole is false, should not call console.log |
| 121 | + expect(consoleLogSpy.called).to.be.false; |
| 122 | + }); |
| 123 | + |
| 124 | + it('should not directly call console.error when forceConsole is false', async () => { |
| 125 | + const logger = new ConsoleLogger('TestContext'); |
| 126 | + |
| 127 | + // Reset spy to ensure clean state |
| 128 | + consoleErrorSpy.resetHistory(); |
| 129 | + |
| 130 | + logger.error('Test error message'); |
| 131 | + |
| 132 | + // When forceConsole is false, should not call console.error |
| 133 | + expect(consoleErrorSpy.called).to.be.false; |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('When forceConsole is set via NestFactory.create', () => { |
| 138 | + it('should apply forceConsole to the default logger', async () => { |
| 139 | + const consoleLogSpy = sinon.spy(console, 'log'); |
| 140 | + const processStdoutSpy = sinon.spy(process.stdout, 'write'); |
| 141 | + |
| 142 | + const moduleRef = await Test.createTestingModule({ |
| 143 | + imports: [AppModule], |
| 144 | + }).compile(); |
| 145 | + |
| 146 | + const testApp = moduleRef.createNestApplication({ |
| 147 | + forceConsole: true, |
| 148 | + }); |
| 149 | + |
| 150 | + await testApp.init(); |
| 151 | + |
| 152 | + // The logger created by NestFactory should respect forceConsole option |
| 153 | + const logger = new ConsoleLogger('AppContext', { forceConsole: true }); |
| 154 | + logger.log('Application started'); |
| 155 | + |
| 156 | + expect(consoleLogSpy.called).to.be.true; |
| 157 | + |
| 158 | + consoleLogSpy.restore(); |
| 159 | + processStdoutSpy.restore(); |
| 160 | + await testApp.close(); |
| 161 | + }); |
| 162 | + }); |
| 163 | +}); |
0 commit comments