michaelbromley / ngx-pagination

Pagination for Angular
http://michaelbromley.github.io/ngx-pagination/
MIT License
1.21k stars 245 forks source link

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

Closed Doaa-Ahmed closed 1 month ago

Doaa-Ahmed commented 1 month ago

Angular version: 14.2.0

ngx-pagination version: 5.1.1

Description of issue: 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.

Steps to reproduce: This is my .spec.ts file:

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

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();
});

});`

Expected result: The test suite to run successfully.

Actual result: 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`

Doaa-Ahmed commented 1 month ago

I have updated the version to [6.0.3] and now the tests are running