I have a very simple hello world example service - i want to test it, passing in our own stubbed config service (to show how the thing works).
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class HelloWorldService {
private readonly logger = new Logger(HelloWorldService.name);
private logPrefix = '';
constructor(private readonly configService: ConfigService) {
this.logger.log('HelloWorldService constructor', { configService });
console.log('HelloWorldService constructor', { configService });
this.logPrefix = this.configService.get('HelloWorldService.logPrefix') ?? ' x ';
this.logger.log('logPrefix', { logPrefix: this.logPrefix });
console.log('HelloWorldService constructor', { logPrefix: this.logPrefix });
}
/**
* Returns a greeting message
* @param name - The name to greet
* @returns A greeting string
*/
hello(name = 'World'): string {
const response = `${this.logPrefix}Hello, ${name}!`;
this.logger.log(response);
return response;
}
/**
* Returns a farewell message
* @param name - The name to bid farewell to
* @returns A farewell string
*/
goodbye(name = 'World'): string {
const response = `${this.logPrefix}Goodbye, ${name}!`;
this.logger.log(response);
return response;
}
}
Yet, nothing i've tried has worked. Seems like nothing is passed to the service, as i always see:
HelloWorldService constructor { configService: undefined }
- sample spec in first comment.