I'm encountering an issue with mocking the return value of a provider, as it seems to be clearing out the mock unexpectedly.
Module1.ts
@Module({
providers: [Service1],
exports: [Service1],
})
export class Module1 {}
Service1.ts
@Injectable({
scope: Scope.REQUEST,
})
export class Service1 {
constructor() {
}
public getVal() {
return '3';
}
}
Service2.ts
@Injectable()
export class Service2 {
constructor(private readonly service1: Service1) {}
private someFn() {
this.service1.getVal();
}
}
Service2.test.ts
let service1;
let service2;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [Module1],
providers: [Service2],
}).compile();
service2= await module.resolve(Service2);
service1= await module.resolve(Service1);
});
it('some test',()=> {
jest.spyOn(service1,'getVal').mockReturnValue('2'); //This should mock getVal function and return 2.
service2.someFn();
});
The issue is that the mock is not being applied. Is there something I am missing?