I have a nrwl/nx workspace with a library called missions-shared This library has a feature state, under the key missions I also have a backend service, called BackendServiceClient. This backend returns promises (due to code generation)
My problem is with the fact that the backend returns promises, and jasmine-marbles being unable to recognize that:
I'm trying to mock the backend. I use a test provider for that
let backend = {
get: () => Promise.resolve({items: []})
}
TestBed.configureTestingModule({
...
providers : [{
provide: BackendServiceClient, useValue: backend
}]
...
This is the MissionsEffect :
@Injectable()
export class MissionsEffects {
@Effect() loadMissions$ = this.dataPersistence.fetch(
MissionsActionTypes.LoadMissions,
{
run: (action: LoadMissions, state: MissionsPartialState) => {
return from(this.backend.get(new GetMissions())).pipe(
map(r => new MissionsLoaded(r.items))
);
},
onError: (action: LoadMissions, error) => {
console.error('Error', error);
return new MissionsLoadError(error);
}
}
);
constructor(
private dataPersistence: DataPersistence<MissionsPartialState>,
private backend: BackendClientService
) {}
}
Then I try to test these @Effect using jasmine-marbles
actions = hot('-a-|', { a: new LoadMissions() });
expect(effects.loadMissions$).toBeObservable(
hot('-a-|', { a: new MissionsLoaded([]) })
);
But I get this error in the tests :
● MissionsEffects › loadMissions$ › should work
expect(received).toEqual(expected) // deep equality
- Expected
+ Received
- Array [
- Object {
- "frame": 10,
- "notification": Notification {
- "error": undefined,
- "hasValue": true,
- "kind": "N",
- "value": MissionsLoaded {
- "payload": Array [],
- "type": "[Missions] Missions Loaded",
- },
- },
- },
- Object {
- "frame": 30,
- "notification": Notification {
- "error": undefined,
- "hasValue": false,
- "kind": "C",
- "value": undefined,
- },
- },
- ]
+ Array []
It seems like because I'm mocking the backend using a Promise (as it should), it doesn't recognize the returned Observable.
If I change the mock to :
let backend = {
get: () => of({items: []})
}
Then the test succeeds.
This is the test :
describe('MissionsEffects', () => {
let actions: Observable<any>;
let effects: MissionsEffects;
let backend = {
get: () => of({items: []})
}
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
NxModule.forRoot(),
StoreModule.forRoot({}),
EffectsModule.forRoot([]),
RouterModule.forRoot([])
],
providers: [
MissionsEffects,
DataPersistence,
provideMockActions(() => actions),
{ provide: APP_BASE_HREF, useValue: '/' },
{ provide: BackendClientService, useValue: backend }
]
});
effects = TestBed.get(MissionsEffects);
});
describe('loadMissions$', () => {
it('should work', () => {
actions = hot('-a-|', { a: new LoadMissions() });
expect(effects.loadMissions$).toBeObservable(
hot('-a-|', { a: new MissionsLoaded([]) })
);
});
});
});
I can confirm that the issue is not due to using @nrwl/DataPersistence, as the following @Effect produces the same error:
@Injectable()
export class MissionsEffects {
@Effect() loadMissions$ = this.actions$
.pipe(
ofType(MissionsActionTypes.LoadMissions),
switchMap(loadMission => {
return from(this.backend.get(new GetMissions())
.then(r => new MissionsLoaded(r.items))
.catch(e => new MissionsLoadError(e)))
})
)
constructor(
private actions$: Actions,
private backend: BackendClientService
) {}
}
Can someone help me understand exactly what is the problem here? Why can't I use mocked promises to test this?