Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 10 | ngrx Effects | Unit Tests | Cannot call a class as a function

After upgrading the Angular project to Angular 10, ngRx effects unit tests started breaking. Following is the error after upgrade:

enter image description here

Running into the same issue, Follow the solution.

Thanks,

like image 477
Manish Kumar Avatar asked Jul 16 '20 18:07

Manish Kumar


People also ask

How to unit test ngrx effects?

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.

What is the angular 10 ngrx store?

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:

How to configure the effectstestingmodule in angular?

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:

How to make API calls through services in angular?

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.


2 Answers

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 targetis 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:

enter image description here

Happy coding and debugging :-)

Thanks,

like image 188
Manish Kumar Avatar answered Oct 16 '22 15:10

Manish Kumar


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).

like image 26
daniel-sc Avatar answered Oct 16 '22 14:10

daniel-sc