In an Angular 10 app I have a service class:
<code>@Injectable()
export class MyService {
private readonly bSubject1$: BehaviorSubject<SomeType[]> = new BehaviorSubject<SomeType[]>(null);
private readonly bSubject2$: BehaviorSubject<AnotherType[]> = new BehaviorSubject<AnotherType[]>(null);
private readonly bSubject3$: BehaviorSubject<LastType[]> = new BehaviorSubject<LastType[]>(null);
private readonly subscription$: Observable<SomeType[]>;
private readonly bs4$: BehaviorSubject<string> = new BehaviorSubject<string>('');
private readonly bs5$ = new BehaviorSubject<boolean>(false);
constructor(private dep1: Dep1Service) {
this.subscription$ = this.createStructure();
}
private createStructure() {
const sub$ = combineLatest([this.bSubject1$, this.bSubject2$, this.bSubject3$])
.pipe(filter(...), map(...)); // Failed: Cannot read properties of undefined (reading 'pipe')
return combineLatest([sub$, this.bs4$, this.bs5$])
.pipe(map(...));
}
}
</code>
<code>@Injectable()
export class MyService {
private readonly bSubject1$: BehaviorSubject<SomeType[]> = new BehaviorSubject<SomeType[]>(null);
private readonly bSubject2$: BehaviorSubject<AnotherType[]> = new BehaviorSubject<AnotherType[]>(null);
private readonly bSubject3$: BehaviorSubject<LastType[]> = new BehaviorSubject<LastType[]>(null);
private readonly subscription$: Observable<SomeType[]>;
private readonly bs4$: BehaviorSubject<string> = new BehaviorSubject<string>('');
private readonly bs5$ = new BehaviorSubject<boolean>(false);
constructor(private dep1: Dep1Service) {
this.subscription$ = this.createStructure();
}
private createStructure() {
const sub$ = combineLatest([this.bSubject1$, this.bSubject2$, this.bSubject3$])
.pipe(filter(...), map(...)); // Failed: Cannot read properties of undefined (reading 'pipe')
return combineLatest([sub$, this.bs4$, this.bs5$])
.pipe(map(...));
}
}
</code>
@Injectable()
export class MyService {
private readonly bSubject1$: BehaviorSubject<SomeType[]> = new BehaviorSubject<SomeType[]>(null);
private readonly bSubject2$: BehaviorSubject<AnotherType[]> = new BehaviorSubject<AnotherType[]>(null);
private readonly bSubject3$: BehaviorSubject<LastType[]> = new BehaviorSubject<LastType[]>(null);
private readonly subscription$: Observable<SomeType[]>;
private readonly bs4$: BehaviorSubject<string> = new BehaviorSubject<string>('');
private readonly bs5$ = new BehaviorSubject<boolean>(false);
constructor(private dep1: Dep1Service) {
this.subscription$ = this.createStructure();
}
private createStructure() {
const sub$ = combineLatest([this.bSubject1$, this.bSubject2$, this.bSubject3$])
.pipe(filter(...), map(...)); // Failed: Cannot read properties of undefined (reading 'pipe')
return combineLatest([sub$, this.bs4$, this.bs5$])
.pipe(map(...));
}
}
Which I’m trying to test like this:
<code>describe('MyService', () => {
let service: MyService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: dep1, useClass: Dep1Service },
{ provide: MyService, useClass: service },
],
});
service = TestBed.inject(MyService);
});
it('should create', () => {
console.log('service', service); // service is logged as `undefined`
expect(service).toBeTruthy();
});
});
</code>
<code>describe('MyService', () => {
let service: MyService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: dep1, useClass: Dep1Service },
{ provide: MyService, useClass: service },
],
});
service = TestBed.inject(MyService);
});
it('should create', () => {
console.log('service', service); // service is logged as `undefined`
expect(service).toBeTruthy();
});
});
</code>
describe('MyService', () => {
let service: MyService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: dep1, useClass: Dep1Service },
{ provide: MyService, useClass: service },
],
});
service = TestBed.inject(MyService);
});
it('should create', () => {
console.log('service', service); // service is logged as `undefined`
expect(service).toBeTruthy();
});
});
And the test fails on the first pipe
with Failed: Cannot read properties of undefined (reading 'pipe')
(highlighted with a comment).
What am I missing in the test mock?