Search code examples
angularunit-testingjestjsngx-pagination

My Jest unit test with Angular is not working when using ngx-pagination


I have a component that uses the Paginate pipe from ngx-pagination in its template. When I try to test this component a simple (should create) using Jest it gives an error in the PaginatePipe_Factory although I added the NgxPaginationModule to the imports of configureTestingModule of Testbed

This is my .spec.ts file:

describe('DeviceSitesListComponent', () => {
    let component: DeviceSitesListComponent;
    let fixture: ComponentFixture<DeviceSitesListComponent>;

    beforeEach(async () => {
        await TestBed.configureTestingModule({
            declarations: [DeviceSitesListComponent, PaginatePipe],
            providers: [DevicesService],
            imports: [
                HttpClientModule, RouterTestingModule, ReactiveFormsModule, TranslateModule.forRoot(),
                ToastrModule.forRoot(), MatSidenavModule, BrowserAnimationsModule, FormsModule, MatPaginatorModule, NgxPaginationModule
            ]
        })
            .compileComponents();
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(DeviceSitesListComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });
});

This is the error I am getting once I add the NgxPaginationModule to the imports.

NG0202: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
    This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.

    Please check that 1) the type for the parameter at index 0 is correct and 2) the correct Angular decorators 
are defined for this class and its ancestors.

      at ɵɵinvalidFactoryDep (node_modules/@angular/core/fesm2020/core.mjs:4798:11)
      at PaginatePipe_Factory (ng:\PaginatePipe\ɵfac.js:5:40)
      at ɵɵpipe (node_modules/@angular/core/fesm2020/core.mjs:22631:30)
      at DeviceSitesListComponent_Template (ng:/DeviceSitesListComponent.js:348:9)
      at executeTemplate (node_modules/@angular/core/fesm2020/core.mjs:12114:9)
      at renderView (node_modules/@angular/core/fesm2020/core.mjs:11914:13)
      at renderComponent (node_modules/@angular/core/fesm2020/core.mjs:13122:5)
      at renderChildComponents (node_modules/@angular/core/fesm2020/core.mjs:11773:9)
      at renderView (node_modules/@angular/core/fesm2020/core.mjs:11939:13)
      at ComponentFactory.create (node_modules/@angular/core/fesm2020/core.mjs:13926:13)
      at initComponent (node_modules/@angular/core/fesm2020/testing.mjs:26284:51)
      at _ZoneDelegate.Object.<anonymous>._ZoneDelegate.invoke (node_modules/zone.js/bundles/zone.umd.js:409:30)      at ProxyZoneSpec.Object.<anonymous>.ProxyZoneSpec.onInvoke (node_modules/zone.js/bundles/zone-testing.umd.js:303:43)
      at _ZoneDelegate.Object.<anonymous>._ZoneDelegate.invoke (node_modules/zone.js/bundles/zone.umd.js:408:56)      at Object.onInvoke (node_modules/@angular/core/fesm2020/core.mjs:26291:33)
      at _ZoneDelegate.Object.<anonymous>._ZoneDelegate.invoke (node_modules/zone.js/bundles/zone.umd.js:408:56)      at Zone.Object.<anonymous>.Zone.run (node_modules/zone.js/bundles/zone.umd.js:169:47)
      at NgZone.run (node_modules/@angular/core/fesm2020/core.mjs:26145:28)
      at TestBedImpl.createComponent (node_modules/@angular/core/fesm2020/testing.mjs:26287:41)
      at Function.createComponent (node_modules/@angular/core/fesm2020/testing.mjs:26095:37)
      at src/app/modules/administration/device-management/components/device-sites/device-sites-list/device-sites-list.component.spec.ts:33:23
      at _ZoneDelegate.Object.<anonymous>._ZoneDelegate.invoke (node_modules/zone.js/bundles/zone.umd.js:409:30)      at ProxyZoneSpec.Object.<anonymous>.ProxyZoneSpec.onInvoke (node_modules/zone.js/bundles/zone-testing.umd.js:303:43)
      at _ZoneDelegate.Object.<anonymous>._ZoneDelegate.invoke (node_modules/zone.js/bundles/zone.umd.js:408:56)      at Zone.Object.<anonymous>.Zone.run (node_modules/zone.js/bundles/zone.umd.js:169:47)
      at Object.wrappedFunc (node_modules/zone.js/bundles/zone-testing.umd.js:803:34)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        5.512 s

Solution

  • I have updated my Ngx-Pagination version and the tests are running now