/* * Copyright (C) 2018, 2019 StApps * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ import * as chai from 'chai'; import {expect} from 'chai'; import * as chaiAsPromised from 'chai-as-promised'; import {slow, suite, test, timeout} from 'mocha-typescript'; import * as sinon from 'sinon'; import {Logger} from '../src/Logger'; import {DummyTransport} from './Transport.spec'; chai.should(); chai.use(chaiAsPromised); @suite(timeout(2000), slow(1000)) export class LoggerSpec { @test async error() { const stub = sinon.stub(console, 'error'); await Logger.error('Foobar'); stub.restore(); expect(stub.args[0][0]).contains('[ERROR]'); expect(stub.args[0][0]).contains('Foobar'); } @test async errorInProductiveEnvironment() { const errorStub = sinon.stub(console, 'error'); const nodeEnv = process.env.NODE_ENV; process.env.NODE_ENV = 'production'; await Logger.error('Foobar').should.be.rejectedWith(Error); expect(errorStub.callCount).to.be.equal(1); process.env.ALLOW_NO_TRANSPORT = 'true'; await Logger.error('Foobar').should.not.be.rejectedWith(Error); expect(errorStub.callCount).to.be.equal(2); delete process.env.ALLOW_NO_TRANSPORT; Logger.setTransport(new DummyTransport()); await Logger.error('Foobar').should.not.be.rejectedWith(Error); expect(errorStub.callCount).to.be.equal(3); Logger.setTransport(); process.env.NODE_ENV = nodeEnv; errorStub.restore(); } @test async errorNoOutput() { const stub = sinon.stub(console, 'error'); process.env.STAPPS_LOG_LEVEL = '0'; await Logger.error('Foobar'); stub.restore(); delete process.env.STAPPS_LOG_LEVEL; return expect(stub.called).to.be.false; } @test async errorWithError() { const e = new Error(); const stub = sinon.stub(console, 'error'); await Logger.error(e); stub.restore(); expect(stub.args[0][0]).contains('Error').contains('at').contains(process.cwd()); } @test getLogLevel() { const savedProcess = process; // @ts-ignore process = undefined; (global as any).window = { STAPPS_LOG_LEVEL: 0, }; const stub = sinon.stub(console, 'info'); Logger.info('Foobar'); stub.restore(); process = savedProcess; delete (global as any).window; return expect(stub.called).to.be.false; } @test info() { const stub = sinon.stub(console, 'info'); Logger.info('Foobar'); stub.restore(); expect(stub.args[0][0]).contains('[INFO]'); expect(stub.args[0][0]).contains('Foobar'); } @test infoNoOutput() { const stub = sinon.stub(console, 'info'); process.env.STAPPS_LOG_LEVEL = '0'; Logger.info('Foobar'); stub.restore(); delete process.env.STAPPS_LOG_LEVEL; return expect(stub.called).to.be.false; } @test initialized() { Logger.setTransport(new DummyTransport()); expect(() => { Logger.initialized(); }).not.to.throw(); Logger.setTransport(); } @test initializedInProductiveEnvironment() { const nodeEnv = process.env.NODE_ENV; process.env.NODE_ENV = 'production'; Logger.setTransport(new DummyTransport()); expect(() => { Logger.initialized(); }).not.to.throw(); Logger.setTransport(); expect(() => { Logger.initialized(); }).to.throw(); const stub = sinon.stub(console, 'warn'); process.env.ALLOW_NO_TRANSPORT = 'true'; expect(() => { Logger.initialized(); }).not.to.throw(); delete process.env.ALLOW_NO_TRANSPORT; expect(stub.called).to.be.equal(true); stub.restore(); process.env.NODE_ENV = nodeEnv; } @test log() { const stub = sinon.stub(console, 'log'); Logger.log('Foobar'); stub.restore(); expect(stub.args[0][0]).contains('[LOG]'); expect(stub.args[0][0]).contains('Foobar'); } @test logNoOutput() { const stub = sinon.stub(console, 'log'); process.env.STAPPS_LOG_LEVEL = '0'; Logger.log('Foobar'); stub.restore(); delete process.env.STAPPS_LOG_LEVEL; return expect(stub.called).to.be.false; } @test ok() { const stub = sinon.stub(console, 'log'); Logger.ok('Foobar'); stub.restore(); expect(stub.args[0][0]).contains('[OK]'); expect(stub.args[0][0]).contains('Foobar'); } @test okNoOutput() { const stub = sinon.stub(console, 'log'); process.env.STAPPS_LOG_LEVEL = '0'; Logger.ok('Foobar'); stub.restore(); delete process.env.STAPPS_LOG_LEVEL; return expect(stub.called).to.be.false; } @test setTransport() { expect(() => { Logger.setTransport(new DummyTransport()); Logger.setTransport(); }).not.to.throw(); } @test stringifyArguments() { const stub = sinon.stub(console, 'log'); Logger.log('foo', 'bar'); stub.restore(); expect(stub.args[0][0]).contains('[LOG]'); expect(stub.args[0][0]).contains('foo'); expect(stub.args[0][0]).contains('bar'); } @test warn() { const stub = sinon.stub(console, 'warn'); Logger.warn('Foobar'); stub.restore(); expect(stub.args[0][0]).contains('[WARN]'); expect(stub.args[0][0]).contains('Foobar'); } @test warnNoOutput() { const stub = sinon.stub(console, 'warn'); process.env.STAPPS_LOG_LEVEL = '0'; Logger.warn('Foobar'); stub.restore(); delete process.env.STAPPS_LOG_LEVEL; return expect(stub.called).to.be.false; } }