从Angular 9.x升级到Angular 10.x,使用ngx-quill的component quill-editor的所有组件规格都无法加载。
这是由标准角度测试产生的:
it('should create', () => {
expect(component).toBeTruthy();
});这是它们产生的错误消息:
FAIL my-project src/(...)/my-component.spec.ts
● Test suite failed to run
Call retries were exceeded
at ChildProcessWorker.initialize (node_modules/jest-worker/build/workers/ChildProcessWorker.js:193:21)当我们的视图使用简单的羽毛笔编辑器时,就会发生这种情况:
<quill-editor formControlName="myControlName"></quill-editor>(注释或删除此行将允许测试通过)
以前使用jest.mock调用模拟模块羽毛笔就足够了:
jest.mock('quill');但现在测试失败了..。
我们将QuillModule加载到共享组件中,并根据需要导入此共享组件:
@NgModule({
declarations: [],
imports: [
QuillModule.forRoot({
modules: {
toolbar: [
['bold', 'italic', 'underline', 'strike'],
],
},
}),
],
exports: [QuillModule],
})
export class QuillEditorModule {}发布于 2020-12-18 14:07:43
我们最终使用包装器模块在所有规范文件中使用jest模拟了模块QuillEditorModule:
为了确保它位于..spec.ts文件的顶部,我们能够存根ngx-quill模块及其使用的组件选择器"quill-editor",并且所有的测试都再次通过:
import { QuillEditorModuleStub } from 'src/(my-app-paths)/quill-editor.module.stub';
jest.mock(`src/(my-app-paths)/quill-editor.module`, () => ({
__esModule: true,
QuillEditorModule: QuillEditorModuleStub,
}));存根组件
import { Component, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
@Component({
selector: 'quill-editor',
template: '',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => QuillEditorComponentStub),
multi: true,
},
],
})
export class QuillEditorComponentStub implements ControlValueAccessor {
registerOnChange(fn: any): void {}
registerOnTouched(fn: any): void {}
writeValue(obj: any): void {}
}存根模块:
import { NgModule } from '@angular/core';
import { QuillEditorComponentStub } from './quill-editor-component.stub';
@NgModule({
declarations: [QuillEditorComponentStub],
exports: [QuillEditorComponentStub],
})
export class QuillEditorModuleStub {}https://stackoverflow.com/questions/65350238
复制相似问题