After upgrading the Angular project to Angular 10, ngRx effects unit tests started breaking. Following is the error after upgrade:
Running into the same issue, Follow the solution.
Thanks,
When unit testing NgRx Effects, be aware of the actions that filter through the stream. These will help coordinate which actions should be dispatched in the actions stream. Finally, mock the successful and error responses for side effect. Create tests to handle both successful and unsuccessful actions emitted from the effect.
The Angular NgRx store is a client-side data management pattern that implements the Redux pattern, invented by Facebook, using RxJS observables. As a prequisite, you need to have Node.js and Angular CLI installed on your local development machine, Angular 10 NgRx Store by Example. Open a new command-line interface and run the following commands:
The configureTestingModule method takes an @NgModule -like metadata object. The metadata object can have most of the properties of a normal Angular module. We need to import the EffectsTestingModule from @ngrx/effects. We also need two providers:
In Angular, we can make API calls through services directly. We usually use some kind of state management tool such as NGRX in large applications which makes the overall flow of data among components easier. Your components are only aware of the state and are generally should not be aware of where the NGRX store gets the data.
Hopefully, this can be useful to someone someday.
After multiple hours of debugging and search found the root cause of the issue.
The issue occurs when the target
is set to ES5
in tsconfig.base.json
.
By default, Angular 10 get scaffolded with the setting as ES2015
with which it works fine. But, that solution will not work for legacy browsers ie. IE11.
How do we set the target to ES5
, so that project runs in IE and also the unit tests get passed?
Well, the solution was very simple.
Simply set the target setting in tsconfig.spec.json
to ES2015
(defined below), without changing any configuration in base tsconfig
. That worked like charm for me and the project gets compiled builder with ES5
settings and unit tests run fine with ES2015
setting.
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": ["jasmine"],
"target": "ES2015"
},
"files": ["src/test.ts", "src/polyfills.ts"],
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}
Output:
Happy coding and debugging :-)
Thanks,
In my project this was caused by using TestScheduler.run
- so changing
it('should test some observable', () => {
const scheduler = new TestScheduler((actual, expected) => expect(actual).toEqual(expected));
scheduler.run(helpers => {
const testObservable$ = getYourTestObservable();
helpers.expectObservable(testObservable$).toBe('(a)', {a: 'some value'});
});
});
to
it('should test some observable', fakeAsync(() => {
const testObservable$ = getYourTestObservable();
expect(testObservable$).toBeObservable(cold('(a)', {a: 'some value'}));
}));
resolved this (without altering the compile target).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With