mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-23 10:02:51 +00:00
feat: tests
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
/*
|
||||
* Copyright (C) 2018, 2020 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
@@ -16,41 +17,35 @@ import chai from 'chai';
|
||||
import {expect} from 'chai';
|
||||
import chaiAsPromised from 'chai-as-promised';
|
||||
import chaiSpies from 'chai-spies';
|
||||
import {suite, test} from '@testdeck/mocha';
|
||||
import {Logger} from '../src/logger.js';
|
||||
import {AddLogLevel} from '../src/transformations/add-log-level.js';
|
||||
import {Colorize} from '../src/transformations/colorize.js';
|
||||
import {DummyTransport} from './dummyTransport.js';
|
||||
import {Logger, AddLogLevel, Colorize} from '../src/index.js';
|
||||
import {DummyTransport} from './dummy-transport.js';
|
||||
import path from 'node:path';
|
||||
import chalk from 'chalk';
|
||||
|
||||
chai.should();
|
||||
chai.use(chaiSpies);
|
||||
chai.use(chaiAsPromised);
|
||||
|
||||
@suite()
|
||||
export class LoggerSpec {
|
||||
static 'sandbox': ChaiSpies.Sandbox;
|
||||
chalk.level = 2;
|
||||
|
||||
static 'before'() {
|
||||
LoggerSpec.sandbox = chai.spy.sandbox();
|
||||
}
|
||||
describe('Logger', function () {
|
||||
const sandbox = chai.spy.sandbox();
|
||||
|
||||
'before'() {
|
||||
beforeEach(function () {
|
||||
Logger.setTransformations([new AddLogLevel()]);
|
||||
}
|
||||
});
|
||||
|
||||
'after'() {
|
||||
LoggerSpec.sandbox.restore();
|
||||
}
|
||||
afterEach(function () {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
@test
|
||||
async 'default log level'() {
|
||||
it('should read default log level', async function () {
|
||||
expect((Logger as any).getLevel('LOG')).to.be.equal(31);
|
||||
expect((Logger as any).getLevel('EXIT')).to.be.equal(0);
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
async 'error'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'error', () => {
|
||||
it('should error', async function () {
|
||||
const spy = sandbox.on(console, 'error', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -59,15 +54,14 @@ export class LoggerSpec {
|
||||
expect(spy).to.have.been.called();
|
||||
expect(spy.__spy.calls[0][0]).to.contain('[ERROR]');
|
||||
expect(spy.__spy.calls[0][0]).to.contain('Foobar');
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
async 'error in productive environment'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'error', () => {
|
||||
it('should error in productive environment', async function () {
|
||||
const spy = sandbox.on(console, 'error', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
const nodeEnv = process.env.NODE_ENV;
|
||||
const nodeEnvironment = process.env.NODE_ENV;
|
||||
process.env.NODE_ENV = 'production';
|
||||
|
||||
await Logger.error('Foobar').should.be.rejectedWith(Error);
|
||||
@@ -87,12 +81,11 @@ export class LoggerSpec {
|
||||
|
||||
Logger.setTransport();
|
||||
|
||||
process.env.NODE_ENV = nodeEnv;
|
||||
}
|
||||
process.env.NODE_ENV = nodeEnvironment;
|
||||
});
|
||||
|
||||
@test
|
||||
async 'error without output'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'error', () => {
|
||||
it('should error without output', async function () {
|
||||
const spy = sandbox.on(console, 'error', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -103,24 +96,24 @@ export class LoggerSpec {
|
||||
delete process.env.STAPPS_LOG_LEVEL;
|
||||
|
||||
expect(spy).not.to.have.been.called();
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
async 'error with Error'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'error', () => {
|
||||
it('should error with Error', async function () {
|
||||
const spy = sandbox.on(console, 'error', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
// eslint-disable-next-line unicorn/error-message
|
||||
await Logger.error(new Error());
|
||||
|
||||
expect(spy).to.have.been.called();
|
||||
expect(spy.__spy.calls[0][0]).to.contain('Error');
|
||||
expect(spy.__spy.calls[0][0]).to.contain(process.cwd());
|
||||
}
|
||||
const directory = process.cwd().replaceAll(path.sep, path.posix.sep);
|
||||
expect(spy.__spy.calls[0][0]).to.contain(directory);
|
||||
});
|
||||
|
||||
@test
|
||||
'info'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'info', () => {
|
||||
it('should info', function () {
|
||||
const spy = sandbox.on(console, 'info', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -129,23 +122,22 @@ export class LoggerSpec {
|
||||
expect(spy).to.have.been.called();
|
||||
expect(spy.__spy.calls[0][0]).to.contain('[INFO]');
|
||||
expect(spy.__spy.calls[0][0]).to.contain('Foobar');
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
async 'exits'() {
|
||||
const infoSpy = LoggerSpec.sandbox.on(console, 'info', () => {
|
||||
it('should exit', async function () {
|
||||
const infoSpy = sandbox.on(console, 'info', () => {
|
||||
// noop
|
||||
});
|
||||
const logSpy = LoggerSpec.sandbox.on(console, 'log', () => {
|
||||
const logSpy = sandbox.on(console, 'log', () => {
|
||||
// noop
|
||||
});
|
||||
const warnSpy = LoggerSpec.sandbox.on(console, 'warn', () => {
|
||||
const warnSpy = sandbox.on(console, 'warn', () => {
|
||||
// noop
|
||||
});
|
||||
const errorSpy = LoggerSpec.sandbox.on(console, 'error', () => {
|
||||
const errorSpy = sandbox.on(console, 'error', () => {
|
||||
// noop
|
||||
});
|
||||
const processSpy = LoggerSpec.sandbox.on(process, 'exit', () => {
|
||||
const processSpy = sandbox.on(process, 'exit', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -173,11 +165,10 @@ export class LoggerSpec {
|
||||
expect(processSpy).to.have.been.called.exactly(5);
|
||||
|
||||
process.env.STAPPS_EXIT_LEVEL = exitLevel;
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'info without output'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'info', () => {
|
||||
it('should info without output', function () {
|
||||
const spy = sandbox.on(console, 'info', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -188,10 +179,9 @@ export class LoggerSpec {
|
||||
delete process.env.STAPPS_LOG_LEVEL;
|
||||
|
||||
expect(spy).not.to.have.been.called;
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'initialized'() {
|
||||
it('should be initialized', function () {
|
||||
Logger.setTransport(new DummyTransport());
|
||||
|
||||
expect(() => {
|
||||
@@ -199,11 +189,10 @@ export class LoggerSpec {
|
||||
}).not.to.throw();
|
||||
|
||||
Logger.setTransport();
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'initialized in productive environment'() {
|
||||
const nodeEnv = process.env.NODE_ENV;
|
||||
it('should be initialized in productive environment', function () {
|
||||
const nodeEnvironment = process.env.NODE_ENV;
|
||||
process.env.NODE_ENV = 'production';
|
||||
|
||||
Logger.setTransport(new DummyTransport());
|
||||
@@ -218,7 +207,7 @@ export class LoggerSpec {
|
||||
Logger.initialized();
|
||||
}).to.throw();
|
||||
|
||||
const spy = LoggerSpec.sandbox.on(console, 'warn', () => {
|
||||
const spy = sandbox.on(console, 'warn', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -232,14 +221,13 @@ export class LoggerSpec {
|
||||
|
||||
expect(spy).to.have.been.called();
|
||||
|
||||
process.env.NODE_ENV = nodeEnv;
|
||||
}
|
||||
process.env.NODE_ENV = nodeEnvironment;
|
||||
});
|
||||
|
||||
@test
|
||||
'is compatible with log aggregation in productive environment'() {
|
||||
it('should be compatible with log aggregation in productive environment', function () {
|
||||
Logger.setTransformations([new AddLogLevel(), new Colorize()]);
|
||||
|
||||
let spy = LoggerSpec.sandbox.on(console, 'log', () => {
|
||||
const spy = sandbox.on(console, 'log', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -248,7 +236,7 @@ export class LoggerSpec {
|
||||
expect(spy).to.have.been.called.once;
|
||||
expect(spy.__spy.calls[0][0]).to.equal('\u001B[37m[LOG] Foo\u001B[39m\n\u001B[37mbar\u001B[39m');
|
||||
|
||||
const nodeEnv = process.env.NODE_ENV;
|
||||
const nodeEnvironment = process.env.NODE_ENV;
|
||||
process.env.NODE_ENV = 'production';
|
||||
process.env.ALLOW_NO_TRANSPORT = 'true';
|
||||
|
||||
@@ -259,13 +247,12 @@ export class LoggerSpec {
|
||||
expect(spy).to.have.been.called.twice;
|
||||
expect(spy.__spy.calls[1][0]).to.equal('[LOG] Foo bar');
|
||||
|
||||
process.env.NODE_ENV = nodeEnv;
|
||||
process.env.NODE_ENV = nodeEnvironment;
|
||||
delete process.env.ALLOW_NO_TRANSPORT;
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'log'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
|
||||
it('should log', function () {
|
||||
const spy = sandbox.on(console, 'log', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -274,11 +261,10 @@ export class LoggerSpec {
|
||||
expect(spy).to.have.been.called();
|
||||
expect(spy.__spy.calls[0][0]).to.contain('[LOG]');
|
||||
expect(spy.__spy.calls[0][0]).to.contain('Foobar');
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'log without output'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
|
||||
it('should log without output', function () {
|
||||
const spy = sandbox.on(console, 'log', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -289,11 +275,10 @@ export class LoggerSpec {
|
||||
delete process.env.STAPPS_LOG_LEVEL;
|
||||
|
||||
expect(spy).to.not.have.been.called();
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'ok'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
|
||||
it('should ok', function () {
|
||||
const spy = sandbox.on(console, 'log', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -302,11 +287,10 @@ export class LoggerSpec {
|
||||
expect(spy).to.have.been.called();
|
||||
expect(spy.__spy.calls[0][0]).to.contain('[OK]');
|
||||
expect(spy.__spy.calls[0][0]).to.contain('Foobar');
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'ok without output'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
|
||||
it('should ok without output', function () {
|
||||
const spy = sandbox.on(console, 'log', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -317,19 +301,17 @@ export class LoggerSpec {
|
||||
delete process.env.STAPPS_LOG_LEVEL;
|
||||
|
||||
expect(spy).not.to.have.been.called();
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'setTransport'() {
|
||||
it('should set transport', function () {
|
||||
expect(() => {
|
||||
Logger.setTransport(new DummyTransport());
|
||||
Logger.setTransport();
|
||||
}).not.to.throw();
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'stringify'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
|
||||
it('should stringify', function () {
|
||||
const spy = sandbox.on(console, 'log', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -338,11 +320,10 @@ export class LoggerSpec {
|
||||
expect(spy).to.have.been.called();
|
||||
expect(spy.__spy.calls[0][0]).to.contain('foo');
|
||||
expect(spy.__spy.calls[0][0]).to.contain('bar');
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'stringify object'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
|
||||
it('should stringify object', function () {
|
||||
const spy = sandbox.on(console, 'log', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -353,11 +334,10 @@ export class LoggerSpec {
|
||||
expect(spy).to.have.been.called();
|
||||
expect(spy.__spy.calls[0][0]).to.contain('foo');
|
||||
expect(spy.__spy.calls[0][0]).to.contain('bar');
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'warn'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'warn', () => {
|
||||
it('should warn', function () {
|
||||
const spy = sandbox.on(console, 'warn', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -366,11 +346,10 @@ export class LoggerSpec {
|
||||
expect(spy).to.have.been.called();
|
||||
expect(spy.__spy.calls[0][0]).to.contain('[WARN]');
|
||||
expect(spy.__spy.calls[0][0]).to.contain('Foobar');
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'warn without output'() {
|
||||
const spy = LoggerSpec.sandbox.on(console, 'warn', () => {
|
||||
it('should warn without output', function () {
|
||||
const spy = sandbox.on(console, 'warn', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -381,17 +360,16 @@ export class LoggerSpec {
|
||||
delete process.env.STAPPS_LOG_LEVEL;
|
||||
|
||||
expect(spy).not.to.have.been.called();
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'log level exclusiveness'() {
|
||||
const warnSpy = LoggerSpec.sandbox.on(console, 'warn', () => {
|
||||
it('should log level exclusiveness', function () {
|
||||
const warnSpy = sandbox.on(console, 'warn', () => {
|
||||
// noop WARN
|
||||
});
|
||||
const infoSpy = LoggerSpec.sandbox.on(console, 'info', () => {
|
||||
const infoSpy = sandbox.on(console, 'info', () => {
|
||||
// noop INFO
|
||||
});
|
||||
const okSpy = LoggerSpec.sandbox.on(console, 'log', () => {
|
||||
const okSpy = sandbox.on(console, 'log', () => {
|
||||
// noop OK
|
||||
});
|
||||
|
||||
@@ -413,20 +391,19 @@ export class LoggerSpec {
|
||||
expect(okSpy).to.not.have.been.called();
|
||||
|
||||
delete process.env.STAPPS_LOG_LEVEL;
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'getExitLevel'() {
|
||||
it('should getExitLevel', function () {
|
||||
const savedProcess = process;
|
||||
|
||||
// @ts-ignore
|
||||
// @ts-expect-error ignore
|
||||
process = undefined;
|
||||
|
||||
(global as any).window = {
|
||||
STAPPS_EXIT_LEVEL: 0,
|
||||
};
|
||||
|
||||
const stub = LoggerSpec.sandbox.on(console, 'info', () => {
|
||||
const stub = sandbox.on(console, 'info', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -437,20 +414,19 @@ export class LoggerSpec {
|
||||
delete (global as any).window;
|
||||
|
||||
expect(stub).not.to.have.been.called();
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'getLogLevel'() {
|
||||
it('should getLogLevel', function () {
|
||||
const savedProcess = process;
|
||||
|
||||
// @ts-ignore
|
||||
// @ts-expect-error ignore
|
||||
process = undefined;
|
||||
|
||||
(global as any).window = {
|
||||
STAPPS_LOG_LEVEL: 0,
|
||||
};
|
||||
|
||||
const stub = LoggerSpec.sandbox.on(console, 'info', () => {
|
||||
const stub = sandbox.on(console, 'info', () => {
|
||||
// noop
|
||||
});
|
||||
|
||||
@@ -461,20 +437,19 @@ export class LoggerSpec {
|
||||
delete (global as any).window;
|
||||
|
||||
expect(stub).not.to.have.been.called();
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
'output without transformations'() {
|
||||
it('should output without transformations', function () {
|
||||
Logger.setTransformations([]);
|
||||
|
||||
const stub = LoggerSpec.sandbox.on(console, 'log', () => {
|
||||
const stub = sandbox.on(console, 'log', () => {
|
||||
// noop
|
||||
});
|
||||
const applyTransformationsSpy = LoggerSpec.sandbox.on(new Logger(), 'applyTransformations');
|
||||
const applyTransformationsSpy = sandbox.on(new Logger(), 'applyTransformations');
|
||||
|
||||
Logger.log('Foobar');
|
||||
|
||||
expect(stub).to.have.been.called.with('Foobar');
|
||||
expect(applyTransformationsSpy).not.to.have.been.called;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user