Skip to content

Commit

Permalink
test: increase test coverage and extract test-util library (#22)
Browse files Browse the repository at this point in the history
* test: add coverage to log drivers and log levels

* test: increase coverage for log types

* chore: git ignore debug.log files

* test: use resolveDependency

* test: extract test-util library

* test: refactor test util library
  • Loading branch information
LayZeeDK authored Oct 19, 2020
1 parent b3bc489 commit 6cb159c
Show file tree
Hide file tree
Showing 24 changed files with 351 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ npm-debug.log
yarn-error.log
testem.log
/typings
debug.log

# System Files
.DS_Store
Expand Down
26 changes: 26 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,32 @@
}
}
}
},
"test-util": {
"projectType": "library",
"root": "projects/internal/test-util",
"sourceRoot": "projects/internal/test-util/src",
"prefix": "lib",
"architect": {
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "projects/internal/test-util/src/test.ts",
"tsConfig": "projects/internal/test-util/tsconfig.spec.json",
"karmaConfig": "projects/internal/test-util/karma.conf.js"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"projects/internal/test-util/tsconfig.lib.json",
"projects/internal/test-util/tsconfig.spec.json"
],
"exclude": ["**/node_modules/**"]
}
}
}
}
},
"defaultProject": "lumberjack"
Expand Down
5 changes: 5 additions & 0 deletions projects/internal/test-util/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Test utilities

## Running unit tests

Run `ng test test-util` to execute the unit tests.
32 changes: 32 additions & 0 deletions projects/internal/test-util/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma'),
],
client: {
clearContext: false, // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, '../../../coverage/test-util'),
reports: ['html', 'lcovonly', 'text-summary'],
fixWebpackSourcePaths: true,
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
restartOnFileChange: true,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { TestBed } from '@angular/core/testing';

import { resolveDependency } from './resolve-dependency';

/**
* Assert that an NgModule is guarded against direct import.
*/
export const expectNgModuleToBeGuarded = <TModule>(ngModuleType: Type<TModule>) => {
let ngModule: TModule | undefined;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { NgModule, Optional, SkipSelf } from '@angular/core';

import { defaultLogDriverConfig, LogDriverConfigToken, LogDriverToken } from '@ngworker/lumberjack';

import { NoopDriver } from './noop-driver.service';

@NgModule({
providers: [
{
provide: LogDriverConfigToken,
useValue: defaultLogDriverConfig,
},
{
provide: LogDriverToken,
useClass: NoopDriver,
multi: true,
},
],
})
export class NoopDriverRootModule {
constructor(@Optional() @SkipSelf() maybeNgModuleFromParentInjector?: NoopDriverRootModule) {
if (maybeNgModuleFromParentInjector) {
throw new Error(
'ConsoleDriverModule.forRoot registered in multiple injectors. Only call it from your root injector such as in AppModule.'
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expectNgModuleToBeGuarded } from '../expect-ng-module-to-be-guarded';

import { NoopDriverModule } from './noop-driver.module';

describe(NoopDriverModule.name, () => {
it('is guarded against direct import', () => {
expectNgModuleToBeGuarded(NoopDriverModule);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ModuleWithProviders, NgModule } from '@angular/core';

import { NoopDriverRootModule } from './noop-driver-root.module';

/**
* Service module for `NoopDriver`.
*
* Use `NoopDriverModule.forRoot` to import.
*/
@NgModule()
export class NoopDriverModule {
static forRoot(): ModuleWithProviders<NoopDriverRootModule> {
return {
ngModule: NoopDriverRootModule,
};
}

constructor() {
throw new Error('Do not import NoopDriverModule directly. Use NoopDriverModule.forRoot.');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Inject, Injectable, ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';

import {
defaultLogDriverConfig,
LogDriver,
LogDriverConfig,
LogDriverConfigToken,
LogDriverToken,
} from '@ngworker/lumberjack';

/**
* No-op log driver.
*/
@Injectable()
export class NoopDriver implements LogDriver {
constructor(@Inject(LogDriverConfigToken) public config: LogDriverConfig) {}
logInfo(logEntry: string): void {}
logDebug(logEntry: string): void {}
logError(logEntry: string): void {}
logWarning(logEntry: string): void {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { TestBed } from '@angular/core/testing';

const inject: Function = (TestBed as any).inject || (TestBed as any).get;

/**
* Resolve dependency from `TestBed`.
*
* Wrapper that prefers `TestBed.inject` over `TestBed.get`. Enables support for
* Angular versions 8 and earlier.
*/
export function resolveDependency<T>(
token: Type<T> | InjectionToken<T> | AbstractType<T>,
notFoundValue?: T,
Expand Down
8 changes: 8 additions & 0 deletions projects/internal/test-util/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Public API Surface of test-util
*/

export * from './lib/expect-ng-module-to-be-guarded';
export * from './lib/noop-driver/noop-driver.module';
export * from './lib/noop-driver/noop-driver.service';
export * from './lib/resolve-dependency';
24 changes: 24 additions & 0 deletions projects/internal/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 projects/internal/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 projects/internal/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"]
},
"files": ["src/test.ts"],
"include": ["**/*.spec.ts", "**/*.d.ts"]
}
7 changes: 7 additions & 0 deletions projects/internal/test-util/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../../tslint.json",
"rules": {
"directive-selector": [true, "attribute", "lib", "camelCase"],
"component-selector": [true, "element", "lib", "kebab-case"]
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TestBed } from '@angular/core/testing';

import { expectNgModuleToBeGuarded, resolveDependency } from '@internal/test-util';
import {
defaultLogDriverConfig,
LogDriver,
Expand All @@ -9,8 +10,6 @@ import {
LumberjackModule,
} from '@ngworker/lumberjack';

import { expectNgModuleToBeGuarded, resolveDependency } from '../../tests';

import { ConsoleDriverModule } from './console-driver.module';
import { ConsoleDriver } from './console.driver';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TestBed } from '@angular/core/testing';

import { expectNgModuleToBeGuarded, resolveDependency } from '@internal/test-util';
import {
defaultLogDriverConfig,
LogDriver,
Expand All @@ -8,8 +9,6 @@ import {
LumberjackModule,
} from '@ngworker/lumberjack';

import { expectNgModuleToBeGuarded, resolveDependency } from '../../tests';

import { HttpDriverConfig } from './http-driver.config';
import { HttpDriverModule } from './http-driver.module';
import { HttpDriver } from './http.driver';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TestBed } from '@angular/core/testing';

import { expectNgModuleToBeGuarded, resolveDependency } from '../../tests';
import { expectNgModuleToBeGuarded, resolveDependency } from '@internal/test-util';

import {
defaultLogConfig,
Expand Down
Loading

0 comments on commit 6cb159c

Please sign in to comment.