|
| 1 | +const winston = require('../../lib/winston'); |
| 2 | +const assume = require('assume'); |
| 3 | + |
| 4 | +// https://github.com/winstonjs/winston/issues/1364 |
| 5 | +describe('transports issue 1364', () => { |
| 6 | + const mainError = 'Error logging!'; |
| 7 | + const otherError = 'Other error'; |
| 8 | + let logger; |
| 9 | + let errorMessage; |
| 10 | + let counter; |
| 11 | + let maxCounter; |
| 12 | + let logError; |
| 13 | + let transport; |
| 14 | + const newTransport = () => |
| 15 | + Object.assign(new winston.transports.Console(), { |
| 16 | + log: (info, next) => { |
| 17 | + if (counter === maxCounter) { |
| 18 | + next(new Error(errorMessage)); |
| 19 | + return; |
| 20 | + } |
| 21 | + if (logError !== null) { |
| 22 | + errorMessage = otherError; |
| 23 | + } |
| 24 | + counter = counter + 1; |
| 25 | + next(); |
| 26 | + return; |
| 27 | + } |
| 28 | + }); |
| 29 | + beforeEach(() => { |
| 30 | + errorMessage = mainError; |
| 31 | + counter = 0; |
| 32 | + maxCounter = 1; |
| 33 | + logError = null; |
| 34 | + transport = newTransport(); |
| 35 | + logger = winston.createLogger({ |
| 36 | + level: 'info', |
| 37 | + transports: [transport] |
| 38 | + }); |
| 39 | + logger.on('error', error => { |
| 40 | + counter = 0; |
| 41 | + logError = error; |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('only log once', () => { |
| 46 | + beforeEach(() => { |
| 47 | + logger.info('log once'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('logger transport has single correct transport', () => { |
| 51 | + const transports = logger.transports; |
| 52 | + assume(transports).is.an('array'); |
| 53 | + assume(transports).length(1); |
| 54 | + assume(transports).contains(transport); |
| 55 | + }); |
| 56 | + |
| 57 | + it("error didn't", () => { |
| 58 | + assume(logError).not.exists(); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('log twice', () => { |
| 63 | + beforeEach(() => { |
| 64 | + logger.info('log twice 1'); |
| 65 | + logger.info('log twice 2'); // this raises the `mainError` for the transport |
| 66 | + }); |
| 67 | + |
| 68 | + it('logger transport has single correct transport', () => { |
| 69 | + const transports = logger.transports; |
| 70 | + assume(transports).is.an('array'); |
| 71 | + assume(transports).length(1); |
| 72 | + assume(transports).contains(transport); |
| 73 | + }); |
| 74 | + |
| 75 | + it('error occurred', () => { |
| 76 | + assume(logError).property('message', mainError); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('log thrice', () => { |
| 81 | + beforeEach(() => { |
| 82 | + logger.info('log thrice 1'); |
| 83 | + logger.info('log thrice 2'); // this raises the `mainError` for the transport |
| 84 | + logger.info('log thrice 3'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('logger transport has single correct transport', () => { |
| 88 | + const transports = logger.transports; |
| 89 | + assume(transports).is.an('array'); |
| 90 | + assume(transports).length(1); |
| 91 | + assume(transports).contains(transport); |
| 92 | + }); |
| 93 | + |
| 94 | + it('error occurred', () => { |
| 95 | + assume(logError).property('message', mainError); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('log four times', () => { |
| 100 | + beforeEach(() => { |
| 101 | + logger.info('log four times 1'); |
| 102 | + logger.info('log four times 2'); // this raises the `mainError` for the transport |
| 103 | + logger.info('log four times 3'); |
| 104 | + logger.info('log four times 4'); // this raises the `otherError` for the transport |
| 105 | + }); |
| 106 | + |
| 107 | + it('logger transport has single correct transport', () => { |
| 108 | + const transports = logger.transports; |
| 109 | + assume(transports).is.an('array'); |
| 110 | + assume(transports).length(1); |
| 111 | + assume(transports).contains(transport); |
| 112 | + }); |
| 113 | + |
| 114 | + it('other error occurred', () => { |
| 115 | + assume(logError).property('message', otherError); |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments