|
1 |
| -'use strict'; |
| 1 | +('use strict'); |
| 2 | + |
| 3 | +import { inspect } from 'util'; |
| 4 | + |
2 | 5 | const os = require('os');
|
3 | 6 | const fs = require('fs');
|
4 | 7 | const { expect } = require('chai');
|
@@ -813,4 +816,73 @@ describe('MongoOptions', function () {
|
813 | 816 | expect(client.options).to.have.property('mongoLoggerOptions').to.equal(expectedLoggingObject);
|
814 | 817 | });
|
815 | 818 | });
|
| 819 | + |
| 820 | + context('when mongodbLogPath is in options', function () { |
| 821 | + const loggerFeatureFlag = Symbol.for('@@mdb.enableMongoLogger'); |
| 822 | + |
| 823 | + let stderrStub; |
| 824 | + let stdoutStub; |
| 825 | + |
| 826 | + beforeEach(() => { |
| 827 | + stdoutStub = sinon.stub(process.stdout); |
| 828 | + stderrStub = sinon.stub(process.stderr); |
| 829 | + }); |
| 830 | + |
| 831 | + afterEach(() => { |
| 832 | + sinon.restore(); |
| 833 | + }); |
| 834 | + |
| 835 | + context('when option is `stderr`', function () { |
| 836 | + it('it is accessible through mongoLogger.logDestination', function () { |
| 837 | + const client = new MongoClient('mongodb://a/', { |
| 838 | + [loggerFeatureFlag]: true, |
| 839 | + mongodbLogPath: 'stderr' |
| 840 | + }); |
| 841 | + const log = { t: new Date(), c: 'constructorStdErr', s: 'error' }; |
| 842 | + client.options.mongoLoggerOptions.logDestination.write(log); |
| 843 | + expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true })); |
| 844 | + }); |
| 845 | + }); |
| 846 | + |
| 847 | + context('when option is a MongoDBLogWritable stream', function () { |
| 848 | + it('it is accessible through mongoLogger.logDestination', function () { |
| 849 | + const writable = { |
| 850 | + buffer: [], |
| 851 | + write(log) { |
| 852 | + this.buffer.push(log); |
| 853 | + } |
| 854 | + }; |
| 855 | + const client = new MongoClient('mongodb://a/', { |
| 856 | + [loggerFeatureFlag]: true, |
| 857 | + mongodbLogPath: writable |
| 858 | + }); |
| 859 | + expect(client.options.mongoLoggerOptions.logDestination).to.deep.equal(writable); |
| 860 | + }); |
| 861 | + }); |
| 862 | + |
| 863 | + context('when option is `stdout`', function () { |
| 864 | + it('it is accessible through mongoLogger.logDestination', function () { |
| 865 | + const client = new MongoClient('mongodb://a/', { |
| 866 | + [loggerFeatureFlag]: true, |
| 867 | + mongodbLogPath: 'stdout' |
| 868 | + }); |
| 869 | + const log = { t: new Date(), c: 'constructorStdOut', s: 'error' }; |
| 870 | + client.options.mongoLoggerOptions.logDestination.write(log); |
| 871 | + expect(stdoutStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true })); |
| 872 | + }); |
| 873 | + }); |
| 874 | + |
| 875 | + context('when option is invalid', function () { |
| 876 | + it('it defaults to stderr', function () { |
| 877 | + const invalidOption = 'stdnothing'; |
| 878 | + const client = new MongoClient('mongodb://a/', { |
| 879 | + [loggerFeatureFlag]: true, |
| 880 | + mongodbLogPath: invalidOption |
| 881 | + }); |
| 882 | + const log = { t: new Date(), c: 'constructorInvalidOption', s: 'error' }; |
| 883 | + client.options.mongoLoggerOptions.logDestination.write(log); |
| 884 | + expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true })); |
| 885 | + }); |
| 886 | + }); |
| 887 | + }); |
816 | 888 | });
|
0 commit comments