Looking for assistance with unit testing my simple logger .ts file. Can someone help me fix the unit test?
logging.service.ts
import 'reflect-metadata'; // Required for tsyringe
import { singleton } from 'tsyringe';
import { Category } from 'typescript-logging';
@singleton()
export class Logger {
private logger: Category;
constructor(){
this.logger = new Category('oidc-security-logger');
}
trace(msg: string): void {
this.logger.trace(msg);
}
debug(msg: string): void {
this.logger.debug(msg);
}
info(msg: string): void {
this.logger.info(msg);
}
warn(msg: string): void {
this.logger.warn(msg);
}
error(msg: string, error?: Error): void {
if(error){
this.logger.error(msg, error);
} else {
this.logger.error(msg, null);
}
}
fatal(msg: string, error?: Error): void {
if (error) {
this.logger.fatal(msg, error);
} else {
this.logger.fatal(msg, null);
}
}
}
I have attempted to complete my unit test using the following method. logging.service.spec.ts
import { Logger } from "./logging.service";
describe("Logger", () => {
let loggerMok: Logger;
beforeEach(() => {
loggerMok = new Logger();
});
it("should be test logger", () => {
const msg='This is for test';
spyOn(loggerMok,'trace').and.stub();
spyOn(loggerMok,'debug').and.stub();
spyOn(loggerMok,'info').and.stub();
spyOn(loggerMok,'warn').and.stub();
spyOn(loggerMok,'error').and.stub();
spyOn(loggerMok,'fatal').and.stub();
loggerMok.trace(msg);
loggerMok.debug(msg);
loggerMok.info(msg);
loggerMok.warn(msg);
loggerMok.error(msg);
loggerMok.fatal(msg);
expect(loggerMok.trace).toHaveBeenCalled();
expect(loggerMok.debug).toHaveBeenCalled();
expect(loggerMok.info).toHaveBeenCalled();
expect(loggerMok.warn).toHaveBeenCalled();
expect(loggerMok.error).toHaveBeenCalled();
expect(loggerMok.fatal).toHaveBeenCalled();
});
});
The unit test seems to be functioning correctly, but the code coverage score is not being generated.