Attempting to incorporate a logging service in my project, the name service implies its purpose.
During initialization, a specific implementation of the logging service is selected.
This implementation utilizes the console for logging messages.
Ensuring that the Console
in the constructor is mockable without impacting the global console
object is crucial.
Tests run successfully on Jasmine and the application functions properly, indicating a potential issue with Jest or its settings.
The goal is to inject the Console interface into an Angular service like so:
export class ConsoleLoggerService implements LoggerService {
constructor(public console: Console) { }
}
During test execution:
describe('ConsoleLoggerService', () => {
let service: ConsoleLoggerService;
let consoleMock: Console;
beforeEach(async(() => {
consoleMock = {
log(message?: any, ...optionalParams: any[]) { }
} as Console;
service = new ConsoleLoggerService(consoleMock);
}));
it('should be created', () => {
expect(service).toBeTruthy();
});
});
Encountering this error:
● Test suite failed to run
ReferenceError: Console is not defined
9 | export class ConsoleLoggerService implements LoggerService {
10 |
> 11 | constructor(public console: Console) { }
| ^
Considering that the Console
interface should be globally accessible from @types/node
, why does the test fail?
My development setup includes Angular 9, Jest 24.9, and Nx 9 in the workspace.