l-lin / angular-datatables

DataTables with Angular
https://l-lin.github.io/angular-datatables/
MIT License
1.57k stars 486 forks source link

Unit test cases shows 'TypeError: $(...).DataTable is not a function' #1562

Closed shivajim closed 3 years ago

shivajim commented 3 years ago

Hi Team, I have been writing unit test cases of angular component where I have been added one common component of Datatable, but *spec.ts file is not getting compiled successfully it shows me an error as error properties: Object({ longStack: 'TypeError: $(...).DataTable is not a function


----------------------------------------------------------------- Error ----------------------------------------------------------------------------------------------
Chrome 91.0.4472.77 (Linux x86_64) ERROR
  An error was thrown in afterAll
  error properties: Object({ longStack: 'TypeError: $(...).DataTable is not a function
      at http://localhost:9876/_karma_webpack_/webpack:/node_modules/angular-datatables/__ivy_ngcc__/src/angular-datatables.directive.js:90:1
      at timer (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:2561:1)
      at ZoneDelegate.invokeTask (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:406:1)
      at ProxyZoneSpec.push.QpwO.ProxyZoneSpec.onInvokeTask (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:332:1)

----------------------------------------------------------------- .*spec.ts  ---------------------------------------------------------------------------------------
// Adeed datatable in shared module. 
  beforeEach(async () => {
    employerService = jasmine.createSpyObj('EmployerService', ['fetchALlEmployers', 'searchGroups']);
    await TestBed.configureTestingModule({
      declarations: [SelectEmployerComponent, EmployerSearchPipe, DatatablesComponent],
      imports: [SharedModule, BrowserAnimationsModule, HttpClientTestingModule],
      providers: [EmployerSearchPipe, {provide: EmployerService, useValue: employerService}]
    })
    .compileComponents();
  });

----------------------------------------------------------------- .*component.ts  --------------------------------------------------------------------------------
export class SelectEmployerComponent implements OnInit, AfterViewChecked, OnDestroy {
  @ViewChild(DatatablesComponent, {static: false}) commonDatatable: DatatablesComponent | undefined;

----------------------------------------------------------------- .*datatble.component.ts  -------------------------------------------------------------------
export class DatatablesComponent implements OnInit, AfterViewInit, AfterViewChecked, OnDestroy {
  @Input() tableData: TableData[];
  @ViewChildren(DataTableDirective) dtElements: QueryList | undefined;
  dtOptions: DataTables.Settings[] = [];
  dtTrigger1: Subject = new Subject();
  dtTrigger2: Subject = new Subject();

:globe_with_meridians: Environment

shanmukhateja commented 3 years ago

Hi,

angular-datatables version: 9.0.2 angular version: 11.2.5

Please upgrade angular-datatables package to v11.

We have a draft PR for unit tests #1529 which may help you with this.

shanmukhateja commented 3 years ago

Please provide a reproducible StackBlitz repo or GitHub link to investigate.