Home > Enterprise >  Testing in Angular with Jasmine/Karma & 3rd party dependencies like ExcelJS
Testing in Angular with Jasmine/Karma & 3rd party dependencies like ExcelJS

Time:06-30

I'm working on tests for a project using ExcelJS, in build and in prod it works fine but in trying to add unit tests to a few components I'm noticing the TestBed fails to build for the following errors: Error description

Looking into this it seems like an issue of Jasmine not being able to require the needed node modules for the component to work.

If I comment out all excel js code the testbed and tests run fine.

I've configured my ts.config.app.json like so, enabling type: node and I'm still seeing this issue.

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": ["node"]
  },
  "include": [
    "src/main.ts",
    "src/polyfills.ts",
    "src/**/*.d.ts"
  ],
  "exclude": [
    "src/environments/environment.*.ts",
    "src/test.ts",
    "src/**/*.spec.ts",
    "src/**/testing/*.ts",
    "src/**/testing/performance/*.ts"
  ]

Can anyone provide insight on how to set up tests for components using exceljs? My current TestBed setup is:

describe('ChartComponent', () => {
  let cmp: ChartComponent;
  let fixture: ComponentFixture<ChartComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        HttpClientModule,
        RouterTestingModule,
      ],
      declarations: [ ChartComponent],
      providers: [
        MockProvider(DataService),
        MockProvider(MessageService),
        MockProvider(TranslationService)
      ]
    })
    .compileComponents();

    fixture = TestBed.createComponent(ChartComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

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

... 

CodePudding user response:

You have to add 'node' to the types array in tsconfig.spec.json file and you may have to continue playing with that (maybe have to add other types) to get the other types of ExcelJS happy as well.

  • Related