angulartypescriptunit-testingjasminejodit

Jasmine: Jodit is not defined, Unit testing


I'm trying to test our customized Jodit editor in app but even automatic 'should create' test is failed. Failure message is 'Jodit is not defined'.

jodit.component.ts

import { Component, OnInit, AfterViewInit, OnDestroy, Input, Output, } from '@angular/core';

declare var Jodit: any;

@Component({
  selector: 'app-jodit',
  templateUrl: './jodit.component.html',
  styleUrls: ['./jodit.component.css']
})

export class JoditComponent implements AfterViewInit, OnDestroy {

  @Input() ... ;
  @Output() ... ;

  ngAfterViewInit() {
    this.editor = new Jodit('#' + this.elementId, ...
      });

jodit.component.spec.ts

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { JoditComponent } from './jodit.component';

describe('JoditComponent', () => {

  let fixture: ComponentFixture<JoditComponent>;
  let component: JoditComponent;


  beforeEach(() => {

    TestBed.configureTestingModule({
      declarations: [ JoditComponent ]
    })

    fixture = TestBed.createComponent(JoditComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();

  });

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

I'm really new in Angular and testing at all but i'm wondering may be i've declared Jodit variable false or someting from imports is missed. I'd really appreciate it if someone could help me out with this.


Solution

  • In your .spec.ts file, you have to import Jodit and bundle it the same way you have in your jodit.component.ts. Maybe the imports array of TestBed.configureTestingModule needs it as well.

    Add a typescript library to angular

    Check that out but in your tests you have to import it as well.

    I would use the Angular wrapper though if I were you: https://github.com/jodit/jodit-angular