首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >升级后使用带有Jest的Angular 10.2时,ngx-quill测试失败

升级后使用带有Jest的Angular 10.2时,ngx-quill测试失败
EN

Stack Overflow用户
提问于 2020-12-18 08:30:32
回答 1查看 335关注 0票数 2

从Angular 9.x升级到Angular 10.x,使用ngx-quill的component quill-editor的所有组件规格都无法加载。

这是由标准角度测试产生的:

代码语言:javascript
复制
    it('should create', () => {
        expect(component).toBeTruthy();
    });

这是它们产生的错误消息:

代码语言:javascript
复制
 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)

当我们的视图使用简单的羽毛笔编辑器时,就会发生这种情况:

代码语言:javascript
复制
<quill-editor formControlName="myControlName"></quill-editor>

(注释或删除此行将允许测试通过)

以前使用jest.mock调用模拟模块羽毛笔就足够了:

代码语言:javascript
复制
jest.mock('quill');

但现在测试失败了..。

我们将QuillModule加载到共享组件中,并根据需要导入此共享组件:

代码语言:javascript
复制
@NgModule({
    declarations: [],
    imports: [
        QuillModule.forRoot({
            modules: {
                toolbar: [
                    ['bold', 'italic', 'underline', 'strike'],
                ],
            },
        }),
    ],
    exports: [QuillModule],
})
export class QuillEditorModule {}
EN

回答 1

Stack Overflow用户

发布于 2020-12-18 14:07:43

我们最终使用包装器模块在所有规范文件中使用jest模拟了模块QuillEditorModule

为了确保它位于..spec.ts文件的顶部,我们能够存根ngx-quill模块及其使用的组件选择器"quill-editor",并且所有的测试都再次通过:

代码语言:javascript
复制
import { QuillEditorModuleStub } from 'src/(my-app-paths)/quill-editor.module.stub';

jest.mock(`src/(my-app-paths)/quill-editor.module`, () => ({
    __esModule: true,
    QuillEditorModule: QuillEditorModuleStub,
}));

存根组件

代码语言:javascript
复制
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 {}
}

存根模块:

代码语言:javascript
复制
import { NgModule } from '@angular/core';
import { QuillEditorComponentStub } from './quill-editor-component.stub';

@NgModule({
    declarations: [QuillEditorComponentStub],
    exports: [QuillEditorComponentStub],
})
export class QuillEditorModuleStub {}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65350238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档