Skip to content

Commit

Permalink
feat: add console logger (#38)
Browse files Browse the repository at this point in the history
* feat: add console logger

* chore: keep internal path mappings

* chore: support Node.js 10

* test: fix tests

* feat: rename to LumberjackConsole

* fix: remove barrel to remove circular dependency warning
  • Loading branch information
LayZeeDK authored Nov 3, 2020
1 parent e0b5777 commit d432999
Show file tree
Hide file tree
Showing 35 changed files with 409 additions and 33 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ jobs:
${{ runner.os }}-node-${{ matrix.node-version }}-angular-${{ matrix.angular-version }}-yarn-
- run: yarn install

- run: yarn delete-paths
- run: yarn delete-path-alias @ngworker/lumberjack
- run: yarn delete-path-alias @ngworker/lumberjack/*-driver
- uses: actions/download-artifact@v2
with:
name: lumberjack-package
Expand Down Expand Up @@ -188,7 +189,8 @@ jobs:
${{ runner.os }}-node-${{ matrix.node-version }}-angular-${{ matrix.angular-version }}-yarn-
- run: yarn install

- run: yarn delete-paths
- run: yarn delete-path-alias @ngworker/lumberjack
- run: yarn delete-path-alias @ngworker/lumberjack/*-driver
- uses: actions/download-artifact@v2
with:
name: lumberjack-package
Expand Down
35 changes: 34 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
"projectType": "library",
"root": "libs/internal/test-util",
"sourceRoot": "libs/internal/test-util/src",
"prefix": "lib",
"prefix": "internal",
"architect": {
"test": {
"builder": "@angular-devkit/build-angular:karma",
Expand All @@ -194,6 +194,39 @@
}
}
}
},
"internal-console-driver-test-util": {
"projectType": "library",
"root": "libs/internal/console-driver/test-util",
"sourceRoot": "libs/internal/console-driver/test-util/src",
"prefix": "internal",
"architect": {
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "libs/internal/console-driver/test-util/src/test.ts",
"tsConfig": "libs/internal/console-driver/test-util/tsconfig.spec.json",
"karmaConfig": "libs/internal/console-driver/test-util/karma.conf.js"
},
"configurations": {
"ci": {
"browsers": "ChromeHeadless",
"progress": false,
"watch": false
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"libs/internal/console-driver/test-util/tsconfig.lib.json",
"libs/internal/console-driver/test-util/tsconfig.spec.json"
],
"exclude": ["**/node_modules/**", "!libs/internal/console-driver/test-util/**"]
}
}
}
}
},
"defaultProject": "lumberjack-app"
Expand Down
3 changes: 2 additions & 1 deletion apps/lumberjack-app/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { createComponentFactory, Spectator } from '@ngneat/spectator';

import { NoopConsoleModule } from '@internal/console-driver/test-util';
import { LumberjackLogLevel, LumberjackModule } from '@ngworker/lumberjack';
import { ConsoleDriverModule } from '@ngworker/lumberjack/console-driver';
import { HttpDriverModule } from '@ngworker/lumberjack/http-driver';
Expand All @@ -15,6 +16,7 @@ describe('AppComponent', () => {
HttpClientTestingModule,
LumberjackModule.forRoot(),
ConsoleDriverModule.forRoot(),
NoopConsoleModule,
HttpDriverModule.forRoot({
levels: [LumberjackLogLevel.Error],
logWagonSize: 5,
Expand All @@ -23,7 +25,6 @@ describe('AppComponent', () => {
}),
],
});

beforeEach(() => {
spectator = createComponent();
});
Expand Down
5 changes: 5 additions & 0 deletions libs/internal/console-driver/test-util/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Internal test utilities for console driver

## Running unit tests

Run `ng test internal-console-driver-test-util` to execute the unit tests.
14 changes: 14 additions & 0 deletions libs/internal/console-driver/test-util/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require('path');

const getBaseKarmaConfig = require('../../../../karma.conf');

module.exports = (config) => {
const baseConfig = getBaseKarmaConfig();
config.set({
...baseConfig,
coverageIstanbulReporter: {
...baseConfig.coverageIstanbulReporter,
dir: path.join(__dirname, '../../../../coverage/libs/internal/test-util'),
},
});
};
8 changes: 8 additions & 0 deletions libs/internal/console-driver/test-util/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Public API Surface of @internal/console-driver/test-util
*/

export * from './lib/noop-console/noop-console.module';
export * from './lib/noop-console/noop-console.service';
export * from './lib/spy-console/spy-console.module';
export * from './lib/spy-console/spy-console.service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NgModule, Optional, SkipSelf } from '@angular/core';

import { LumberjackConsoleToken } from '@ngworker/lumberjack/console-driver';

import { NoopConsole } from './noop-console.service';

@NgModule({
providers: [
{
deps: [[new Optional(), new SkipSelf(), NoopConsole]],
provide: LumberjackConsoleToken,
useFactory: (maybeExistingInstance: NoopConsole | null): NoopConsole =>
maybeExistingInstance || new NoopConsole(),
},
],
})
export class NoopConsoleModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Injectable } from '@angular/core';

import { LumberjackConsole } from '@ngworker/lumberjack/console-driver';

/**
* No-op console logger.
*
* Every method is a no-op.
*/
@Injectable()
export class NoopConsole implements LumberjackConsole {
// tslint:disable: no-any
debug(...data: any[]): void;
debug(message?: any, ...optionalParams: any[]): void;
debug(message?: any, ...optionalParams: any[]) {}

error(...data: any[]): void;
error(message?: any, ...optionalParams: any[]): void;
error(message?: any, ...optionalParams: any[]) {}

info(...data: any[]): void;
info(message?: any, ...optionalParams: any[]): void;
info(message?: any, ...optionalParams: any[]) {}

trace(...data: any[]): void;
trace(message?: any, ...optionalParams: any[]): void;
trace(message?: any, ...optionalParams: any[]) {}

warn(...data: any[]): void;
warn(message?: any, ...optionalParams: any[]): void;
warn(message?: any, ...optionalParams: any[]) {}
// tslint:enable: no-any
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NgModule, Optional, SkipSelf } from '@angular/core';

import { LumberjackConsoleToken } from '@ngworker/lumberjack/console-driver';

import { SpyConsole } from './spy-console.service';

@NgModule({
providers: [
{
deps: [[new Optional(), new SkipSelf(), SpyConsole]],
provide: LumberjackConsoleToken,
useFactory: (maybeExistingInstance: SpyConsole | null): SpyConsole => maybeExistingInstance || new SpyConsole(),
},
],
})
export class SpyConsoleModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { SpyConsole } from './spy-console.service';

describe(SpyConsole.name, () => {
beforeEach(() => {
spy = new SpyConsole();
});

let spy: SpyConsole;

it('can be passed a lot of arguments', () => {
const hundredNumbers = Array(100)
.fill(undefined)
.map((_, index) => index + 1);

spy.debug.apply(spy, hundredNumbers);

expect(spy.debug).toHaveBeenCalledTimes(1);
expect(spy.debug).toHaveBeenCalledWith(...hundredNumbers);
});

it('can reset spy tracking', () => {
spy.debug(1);
spy.debug(2);
spy.error(1);
spy.error(2);
spy.error(3);

expect(spy.debug).toHaveBeenCalledTimes(2);
expect(spy.error).toHaveBeenCalledTimes(3);

spy.reset();

expect(spy.debug).toHaveBeenCalledTimes(0);
expect(spy.error).toHaveBeenCalledTimes(0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@angular/core';

import { LumberjackConsole } from '@ngworker/lumberjack/console-driver';

/**
* Spy console logger.
*
* Every method is a spy.
*/
@Injectable()
export class SpyConsole implements LumberjackConsole, jasmine.SpyObj<LumberjackConsole> {
debug = jasmine.createSpy('debug');

error = jasmine.createSpy('error');

info = jasmine.createSpy('info');

trace = jasmine.createSpy('trace');

warn = jasmine.createSpy('warn');

/**
* Reset tracking on spies.
*/
reset(): void {
this.debug.calls.reset();
this.error.calls.reset();
this.info.calls.reset();
this.trace.calls.reset();
this.warn.calls.reset();
}
}
24 changes: 24 additions & 0 deletions libs/internal/console-driver/test-util/src/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import { getTestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import 'zone.js/dist/zone';
import 'zone.js/dist/zone-testing';

declare const require: {
context(
path: string,
deep?: boolean,
filter?: RegExp
): {
keys(): string[];
<T>(id: string): T;
};
};

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
18 changes: 18 additions & 0 deletions libs/internal/console-driver/test-util/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "../../../../tsconfig.json",
"compilerOptions": {
"outDir": "../../../../out-tsc/lib",
"target": "es2015",
"declaration": true,
"inlineSources": true,
"types": [],
"lib": ["dom", "es2018"]
},
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"enableResourceInlining": true
},
"exclude": ["src/test.ts", "**/*.spec.ts"]
}
10 changes: 10 additions & 0 deletions libs/internal/console-driver/test-util/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "../../../../tsconfig.json",
"compilerOptions": {
"outDir": "../../../../out-tsc/spec",
"types": ["jasmine", "node"]
},
"files": ["src/test.ts"],
"include": ["**/*.spec.ts", "**/*.d.ts"]
}
10 changes: 10 additions & 0 deletions libs/internal/console-driver/test-util/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../tslint.json",
"rules": {
"directive-selector": [true, "attribute", "lib", "camelCase"],
"component-selector": [true, "element", "lib", "kebab-case"]
},
"linterOptions": {
"exclude": ["!**/*"]
}
}
3 changes: 1 addition & 2 deletions libs/internal/test-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export * from './lib/expect-ng-module-to-be-guarded';
export * from './lib/logs/log-creators';
export * from './lib/noop-driver/noop-driver.module';
export * from './lib/noop-driver/noop.driver';
export * from './lib/resolve-dependency';
export * from './lib/spy-driver/spy-driver.module';
export * from './lib/spy-driver/spy.driver';
export * from './lib/resolve-dependency';
export * from './lib/array-equality';
6 changes: 0 additions & 6 deletions libs/internal/test-util/src/lib/array-equality.ts

This file was deleted.

2 changes: 1 addition & 1 deletion libs/internal/test-util/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/lib",
"outDir": "../../../out-tsc/lib",
"target": "es2015",
"declaration": true,
"inlineSources": true,
Expand Down
2 changes: 1 addition & 1 deletion libs/internal/test-util/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/spec",
"outDir": "../../../out-tsc/spec",
"types": ["jasmine", "node"]
},
"files": ["src/test.ts"],
Expand Down
2 changes: 2 additions & 0 deletions libs/ngworker/lumberjack/console-driver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
export * from './lib/console-driver-root.module';
export * from './lib/console-driver.module';
export * from './lib/console.driver';
export * from './lib/lumberjack-console';
export * from './lib/lumberjack-console.token';
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const verboseDriverConfig: LogDriverConfig = {
};

describe('ConsoleDriverConfigToken', () => {
describe('given a provided console driver config', () => {
describe('given a provided console log driver config', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
Expand All @@ -30,14 +30,14 @@ describe('ConsoleDriverConfigToken', () => {
});
});

describe('given no provided console driver config', () => {
describe('given no provided console log driver config', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{ provide: LogDriverConfigToken, useValue: verboseDriverConfig }],
});
});

it('then the log driver config is resolved', () => {
it('then the value of the log driver config is resolved', () => {
const actualDriverConfig = resolveDependency(ConsoleDriverConfigToken);

expect(actualDriverConfig).toBe(verboseDriverConfig);
Expand Down
Loading

0 comments on commit d432999

Please sign in to comment.