How to provide Injected constant to service in nestjs when unit testing - unit-testing

I try to create unit tests for my small application. I want to test a service that uses injected configurations and other services.
#Injectable()
export class AuthService {
private readonly localUri: string;
constructor(
#Inject(CORE_CONFIG_TOKEN) private readonly coreConfig: ICoreConfig,
#Inject(PROVIDER_CONFIG_TOKEN) private readonly providerConfig: IProviderConfig,
private readonly _httpService: HttpService,
private readonly _usersService: UsersService,
) {
this.localUri = `http://${this.coreConfig.domain}:${this.coreConfig.port}`;
}
...
/**
* Checks if a given email is already taken
* #param email
*/
async isEmailTaken(email: string): Promise<boolean> {
return (await this._usersService.findUserByEmail(email)) !== undefined;
}
...
I do not understand how to test this service. I don't know how to provide a correct TestModule provider for the injected configuration #Inject(CORE_CONFIG_TOKEN) private readonly coreConfig: ICoreConfig
const testCoreConfig = '{...}'
const module = await Test.createTestingModule({
providers: [AuthService, {
provide: 'CORE_CONFIG_TOKEN',
useClass: testCoreConfig ,
}],
}).compile();
Also I am not sure if I would need also create the other imported services. I just would like to check if they are called. And if so return mock data. This I can do but I am stuck with the module setup.
All samples I found so far where just services with one repository. And more or less checks if the service exists. But no checks against the logic of the implementation and the connections between the classes.
I hope my question is clear
Thank you

I think you should be using the useValue property instead of useClass if you're passing an object of key values?
providers: [
{
provide: CORE_CONFIG_TOKEN,
useValue: {
domain: 'nestjs.com',
port: 80,
},
},
],
There's also some documentation on creating a config provider/service on nestjs for your modules.
I've also created a nestjs-config module you can use similar to this.
#Injectable()
class TestProvider {
constructor(private readonly #InjectConfig() config) {
this.localUri = config.get('local.uri');
}
#Module({
imports: [ConfigModule.load('path/to/config/file/*.ts')],
providers: [TestProvider],
})
export AppModule {}
//config file 'config/local.ts'
export default {
uri: `https://${process.env.DOMAIN}:/${process.env.PORT}`,
}

Related

redis mock for testing in nestjs

I am writing test cases in my email-service.spec.ts file
my email-service file
#Injectable()
export class EmailSubscriptionService {
private nodeTokenCache;
private result;
constructor(#InjectRepository(ConsumerEmailSubscriptions) private readonly emailSubscriptions: Repository<ConsumerEmailSubscriptions>,
#InjectRepository(EmailSubscriptions) private readonly emailSubscriptionLegacy: Repository<EmailSubscriptions>,
#InjectRedisClient('0') private redisClient: Redis.Redis,
private readonly config: ConfigService, private http: HttpService,
private readonly manageSavedSearchService: ManageSavedSearchService) {
}
my email-service.spec.ts file
import { RedisService } from 'nestjs-redis';
import { ConfigService } from '#nestjs/config';
import { HttpService } from '#nestjs/common';
import { ManageSavedSearchService } from './../manage-saved-search/manage-saved-search.service';
describe('EmailSubscriptionService', () => {
let service: EmailSubscriptionService;
let entity : ConsumerEmailSubscriptions;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports:[RedisModule],
// https://github.com/nestjs/nest/issues/1229
providers: [EmailSubscriptionService,
{
// how you provide the injection token in a test instance
provide: getRepositoryToken(ConsumerEmailSubscriptions),
// as a class value, Repository needs no generics
useClass: Repository,
// useValue: {
// }
},
{
provide: getRepositoryToken(EmailSubscriptions),
useClass: Repository,
},
RedisService,
// {
// provide : RedisService,
// useClass: Redis
// },
ConfigService, HttpService, ManageSavedSearchService
],
}).compile();
service = module.get<EmailSubscriptionService>(EmailSubscriptionService);
// entity = module.get<Repository<ConsumerEmailSubscriptions>>(getRepositoryToken(ConsumerEmailSubscriptions));
});
it('should be defined', async () => {
expect(service).toBeDefined;
});
});
result ---->
Nest can't resolve dependencies of the EmailSubscriptionService (ConsumerEmailSubscriptionsRepository, EmailSubscriptionsRepository, ?, ConfigService, HttpService, ManageSavedSearchService). Please make sure that the argument REDIS_CLIENT_PROVIDER_0 at index [2] is available in the RootTestModule context.
I am unable to mock my redisclient in email-service.spec.ts as per the dependency in the service file. I have tried useClass, added RedisService in provide and there are no get-redis methods.
I am able to mock the repositories and for services, I don't know for sure as I am stuck with redis.
Any idea how to mock redis, couldn't find anything in the docs. Also in the next step, will importing the services work or I have to do anything else?
ConfigService, HttpService, manageSavedSearchService: ManageSavedSearchService
If you want to mock the RedisClient in your tests you need to provide the same DI token as what you get back from #InjectRedisClient('0'). This will allow you to replace the redis client for the purposes of your test.
I'm not familiar with the specific Redis library you're using but assuming that it's this one you can take a look at how the token is constructed
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [EmailSubscriptionService,{
provide: EmailSubscriptionService,
useValue: {
getClient: jest.fn(),
}
}],
}).compile();
service = module.get<EmailSubscriptionService>(EmailSubscriptionService);
});
this seems to work somehow, the service class constructor uses many other classes, but using it in provide works..it is kind of counter intuitive as the classes in constructor need to be mocked individually, but without doing that it works.

Retrieving a Provider from TestingModule when using an interface

I am trying to retrieve a provider from a testing module in NestJS, but cannot resolve that module when providing it out of a module using an interface.
This is what I have so far:
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile()
...
const fooService = moduleFixture.get<FooService>(FooService) // Nest could not find FooService element
When the module exporting the FooService is doing something like this...
#Global()
#Module({
providers: [{ provide: 'IFooService', useClass: FooService }],
exports: [{ provide: 'IFooService', useClass: FooService }],
})
export class FooModule {}
How can I get the FooService instance to be able to call methods directly?
You'll need to tell the get method to done the same injection token as provided in your code. In this case
const fooService = moduleFixture.get<FooService>('IFooService');

Angular 2: How to mock ChangeDetectorRef while unit testing

I have just started with Unit-Testing, and I have been able to mock my own services and some of Angular and Ionic as well, but no matter what I do ChangeDetectorRef stays the same.
I mean which kind of sorcery is this?
beforeEach(async(() =>
TestBed.configureTestingModule({
declarations: [MyComponent],
providers: [
Form, DomController, ToastController, AlertController,
PopoverController,
{provide: Platform, useClass: PlatformMock},
{
provide: NavParams,
useValue: new NavParams({data: new PageData().Data})
},
{provide: ChangeDetectorRef, useClass: ChangeDetectorRefMock}
],
imports: [
FormsModule,
ReactiveFormsModule,
IonicModule
],
})
.overrideComponent(MyComponent, {
set: {
providers: [
{provide: ChangeDetectorRef, useClass: ChangeDetectorRefMock},
],
viewProviders: [
{provide: ChangeDetectorRef, useClass: ChangeDetectorRefMock},
]
}
})
.compileComponents()
.then(() => {
let fixture = TestBed.createComponent(MyComponent);
let cmp = fixture.debugElement.componentInstance;
let cdRef = fixture.debugElement.injector.get(ChangeDetectorRef);
console.log(cdRef); // logs ChangeDetectorRefMock
console.log(cmp.cdRef); // logs ChangeDetectorRef , why ??
})
));
it('fails no matter what', async(() => {
spyOn(cdRef, 'markForCheck');
spyOn(cmp.cdRef, 'markForCheck');
cmp.ngOnInit();
expect(cdRef.markForCheck).toHaveBeenCalled(); // fail, why ??
expect(cmp.cdRef.markForCheck).toHaveBeenCalled(); // success
console.log(cdRef); // logs ChangeDetectorRefMock
console.log(cmp.cdRef); // logs ChangeDetectorRef , why ??
}));
#Component({
...
})
export class MyComponent {
constructor(private cdRef: ChangeDetectorRef){}
ngOnInit() {
// do something
this.cdRef.markForCheck();
}
}
I have tried everything , async, fakeAsync, injector([ChangeDetectorRef], () => {}).
Nothing works.
Update 2020:
I wrote this originally in May 2017, it's a solution that worked great at the time and still works.
We can't configure the injection of a changeDetectorRef mock through the test bed, so this is what I am doing these days:
it('detects changes', () => {
// This is a unique instance here, brand new
const changeDetectorRef = fixture.debugElement.injector.get(ChangeDetectorRef);
// So, I am spying directly on the prototype.
const detectChangesSpy = spyOn(changeDetectorRef.constructor.prototype, 'detectChanges');
component.someMethod(); // Which internally calls the detectChanges.
expect(detectChangesSpy).toHaveBeenCalled();
});
Then you don't care about private attributes or any.
In case anyone runs into this, this is one way that has worked well for me:
As you are injecting the ChangeDetectorRef instance in your constructor:
constructor(private cdRef: ChangeDetectorRef) { }
You have that cdRef as one of the private attributes on the component, which means you can spy on the component, stub that attribute and have it return whatever you want. Also, you can assert its calls and parameters, as needed.
In your spec file, call your TestBed without providing the ChangeDetectorRef as it won't provide what you give it. Set the component that same beforeEach block, so it is reset between specs as it is done in the docs here:
component = fixture.componentInstance;
Then in the tests, spy directly on the attribute
describe('someMethod()', () => {
it('calls detect changes', () => {
const spy = spyOn((component as any).cdRef, 'detectChanges');
component.someMethod();
expect(spy).toHaveBeenCalled();
});
});
With the spy you can use .and.returnValue() and have it return whatever you need.
Notice that (component as any) is used as cdRef is a private attribute. But private doesn't exist in the actual compiled javascript so it is accessible.
It is up to you if you want to access private attributes at runtime that way for your tests.
Not sure if this a new thing or not, but changeDetectorRef can be accessed via fixture.
See docs: https://angular.io/guide/testing#componentfixture-properties
We ran into the same issue with change detector mocking and this is ended up being the solution
Probably one point that needs to be pointed out, is that in essence here you want to test your own code, not unit test the change detector itself (which was tested by the Angular team).
In my opinion this is a good indicator that you should extract the call to the change detector to a local private method (private as it is something you don't want to unit test), e.g.
private detectChanges(): void {
this.cdRef.detectChanges();
}
Then, in your unit test, you will want to verify that your code actually called this function, and thus called the method from the ChangeDetectorRef. For example:
it('should call the change detector',
() => {
const spyCDR = spyOn((cmp as any).cdRef, 'detectChanges' as any);
cmp.ngOnInit();
expect(spyCDR).toHaveBeenCalled();
}
);
I had the exact same situation, and this was suggested to me as a general best practice for unit testing from a senior dev who told me that unit testing is actually forcing you by this pattern to structure your code better. With the proposed restructuring, you make sure your code is flexible to change, e.g. if Angular changes the way they provide us with change detection, then you will only have to adapt the detectChanges method.
For unit testing, if you are mocking ChangeDetectorRef just to satisfy dependency injection for a component to be creation, you can pass in any value.
For my case, I did this:
TestBed.configureTestingModule({
providers: [
FormBuilder,
MyComponent,
{ provide: ChangeDetectorRef, useValue: {} }
]
}).compileComponents()
injector = getTestBed()
myComponent = injector.get(MyComponent)
It will create myComponent successfully. Just make sure test execution path does not need ChangeDetectorRef. If you do, then replace useValue: {} with a proper mock object.
In my case, I just needed to test some form creation stuff using FormBuilder.
// component
constructor(private changeDetectorRef: ChangeDetectorRef) {}
public someHandler() {
this.changeDetectorRef.detectChanges();
}
// spec
const changeDetectorRef = fixture.componentRef.changeDetectorRef;
jest.spyOn(changeDetectorRef, 'detectChanges');
fixture.detectChanges(); // <--- needed!!
component.someHandler();
expect(changeDetectorRef.detectChanges).toHaveBeenCalled();

Angular 2 - Mocking Services in Components that depend on other services

How do I mock a service that depends on another service that is in a component?
Please check code below.
a.component.ts
#Component({
selector: 'my-comp',
templateUrl: './my.component.html',
providers: [ MyServiceA ]
})
export class MyComponent {
my-service-a.service.ts
#Injectable()
export class MyServiceA{
constructor(private myServiceB: MyServiceB) {}
my-service-b.service.ts
export class MyServiceB{
constructor(private myServiceC: MyServiceC,
private myServiceD: MyServiceD) {}
How do I mock the service in the a.component.spec.ts in the TestBed configuration? Please help. Thank you.
You can mock it however you want. The other services don't matter. I think maybe the problem you are facing is with the #Component.providers. Using this, any mocks you configure in the TestBed aren't used as the #Component.providers take precedence, causing Angular to try to create it, instead of using the mock.
To get around that, Angular offers the TestBed.overrideComponent method, so that we can override things like the template and providers of the #Component
beforeEach(() => {
let myMockService = new MyMockService();
TestBed.configureTestingModule({
providers: [
// the following will not be used
{ provide: MyService, useValue: myMockService }
]
});
TestBed.overrideComponent(MyComponent, {
set: {
providers: [
// this will override the #Component.providers:[MyService]
{ provide: MyService, useValue: myMockService }
]
}
});
})

angular 2 rc7 - testing service - Error: No provider for String

I've getting an error in angular 2 testing using the webpack quickstart project.
Error: No provider for String! in config/karma-test-shim.js
I've never seen this error where the provider for String is missing. I figured out its related the private url:string in the services constructor but how do I resolve it?
Here's my testfile
describe('http.ts',()=>{
beforeEach(()=>{
TestBed.configureTestingModule({
providers:[
SbHttp,
MockBackend,
BaseRequestOptions,
{ provide: Http, useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
}, deps: [MockBackend, BaseRequestOptions]}
],
imports: [
HttpModule
]
})
});
afterEach(()=>{
TestBed.resetTestingModule()
});
it('get() should work', inject([SbHttp],(sbHttp:SbHttp)=>{
expect(true).toBeTruthy()
}))
})
and here's the SbHttp service
#Injectable()
export class SbHttp{
private baseUrl:string;
constructor( private url:string, private headers:Headers, private http:Http
){
this.baseUrl = utils.stripTrailingSlash(url)
}
}
If I change to private url:any I'm getting this error
Can't resolve all parameters for SbHttp: (?, Headers, Http).
You need to make it injectable by creating a token, using #Inject() to inject with the token, and then add it as a provider in the providers list .
import { OpaqueToken, Injct } from '#angular/core';
const APP_URL = new OpaqueToken('app.url');
class SbHttp {
constructor(#Inject(APP_URL) url: string, ...) {}
}
TestBed.configureTestingModule({
providers: [
{ provide: APP_URL, useValue: 'http://localhost' }
]
});
You'll also need to configure the providers in the real app also. As for the Headers, I'm not sure, but I think you might get the error also once you fix this one. I'm not sure where you expect to be getting that from.
I highly suggest you take a look at the dependency injection chapter from the Angular documentation if you are new to DI with Angular.