UPDATE The simulation for OtherService
was incorrect. You can check the detailed explanation on this answer.
I am trying to identify the issue in the test file that causes it to behave differently from the component. I am puzzled as to why the next:
statement in the test is never executed while it always works in the component, even though they share the same code.
The main goal is to understand why this disparity exists, rather than attempting to refactor the service.
service:
@Injectable({
providedIn: 'root'
})
export class MyService {
strings = ["a","b","c"]
method():Observable<string> {
const subject = new Subject<string>();
let observableArray: Array<Observable<string>> = [];
strings.forEach((data: strings) => {
observableArray.push(
this._otherService
.returnsAnotherObservable()
.pipe(
tap({
next: () => {
console.log("next") // works in both, test & component
subject.next(data);
},
})
)
)
});
forkJoin(observableArray)
.pipe(tap(() => subject.complete()))
.subscribe();
return subject;
}
}
component
...
ngOnInit(): void {
this._myService
.method()
.subscribe({
next: (value: string) => {
console.log(string) // it works
},
complete: () => {
console.log("end")
}
});
}
test
Class MockOtherService{
returnsAnotherObservable():Observable<any> {
// return of(true) will not work because is not async
return of(true).pipe(delay(1000)) // now works
}
}
describe('MyService', () => {
let service: MyService;
beforeEach(async () => {
TestBed.configureTestingModule({
providers: [{provide: OtherService, useClass: MockOtherService }],
imports: [AppModule]
});
service = TestBed.inject(MyService);
});
it("should work", () => {
servie.method()
.subscribe({
next: (value: string) => {
console.log("value", value) // It never reachs here
},
complete: () => {
console.log("end") // it jumps directly here
}
});
});
});
I've tried to play with fakeAsync
& tick()
. I Also change the forkJoin
to zip
without success.
One thig that worked was changing subject()
for BehaviorSubject()
, because yeah, it returns everything always.. which lead me to think that the next()
calls are happening before the test has been subscribed(if that makes sense..) but it's the same for the component.
Thank you