Jasmine:Jodit 未定义,单元测试

Jasmine: Jodit is not defined, Unit testing

我正在尝试在应用程序中测试我们自定义的 Jodit 编辑器,但即使自动 'should create' 测试也失败了。失败消息是 '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();
  }); 
});

我真的是 Angular 中的新手并且根本没有测试,但我想知道我可能已经将 Jodit 变量声明为 false 或导入的某些内容被遗漏了。如果有人能帮我解决这个问题,我将不胜感激。

在您的 .spec.ts 文件中,您必须 import Jodit 并像在 jodit.component.ts 中一样将其捆绑。也许 TestBed.configureTestingModuleimports 数组也需要它。

检查一下,但在您的测试中您还必须导入它。

如果我是你,我会使用 Angular 包装器:https://github.com/jodit/jodit-angular