3

I'm trying to use TestBed from to test a Angular component that has a ng-select in its HTML. I've first got the error saying that Can't bind to 'items' since it isn't a known property of 'ng-select'. so I've imported NgSelectModule and added it to imports in TestBed configurations. It now returns Can't bind to 'ngModel' since it isn't a known property of 'ng-select'

import { getTestBed, TestBed } from '@angular/core/testing';
import { ProductGenericGridComponent } from './product-generic-grid.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ProductGridService } from './product-generic-grid.service';
import { NgSelectModule } from '@ng-select/ng-select';
import { ProductItemComponent } from './../product-item/product-item.component';

describe('Product Generic Grid Component', () => {
    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [ProductGenericGridComponent],
            imports: [HttpClientTestingModule, NgSelectModule],
            providers: []
        });
    });

    afterEach(() => {
        getTestBed().resetTestingModule();
    });

    it('should return true', () => {
        const fixture = TestBed.createComponent(ProductGenericGridComponent);
        expect(true).toBe(true);
    });
});

3 Answers3

9

Import the forms module into your testbed.

    TestBed.configureTestingModule({
        declarations: [ProductGenericGridComponent],
        imports: [HttpClientTestingModule, NgSelectModule, FormsModule],
        providers: []
    });
1

Step 1: Install ng-select: npm install --save @ng-select/ng-select

Step 2: Import the NgSelectModule and angular FormsModule module

import { NgSelectModule } from '@ng-select/ng-select'; import { FormsModule } from '@angular/forms';

Vikas Rao
  • 11
  • 1
0

For me too, it happened while unit testing and the following changes helped to resolve the errors

...
beforeEach((() => {
    TestBed.configureTestingModule({
        imports:[
            MatDialogModule,
            BrowserAnimationsModule,
            FormsModule,
            AgGridModule.withComponents([]),
            NgSelectModule
        ],
...
  • 1
    See [Explaining entirely code-based answers](https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this might be technically correct, it doesn't explain why it solves the problem or should be the selected answer. We should educate along with helping solve the problem. – Gerhard Jun 28 '22 at 13:04
  • @Gerhard yeah, understood. – Aravinth Arjunan Jun 30 '22 at 06:01