我在我的应用程序中使用了@materia-ui/ngx-monaco-editor,并且在monaco
使用 Karma+Jasmine 进行单元测试运行期间无法识别全局安装的库。
零件
import {
Component,
OnInit } from '@angular/core';
import { MonacoEditorLoaderService } from '@materia-ui/ngx-monaco-editor';
import { filter, take } from 'rxjs/operators';
@Component({
selector: 'my-editor',
templateUrl: './my-editor.component.html',
styleUrls: ['./my-editor.component.scss']
})
export class MyEditorComponent implements OnInit {
public modelUri: monaco.Uri;
constructor(private monacoLoaderService: MonacoEditorLoaderService) {
this.monacoLoaderService.isMonacoLoaded$.pipe(
filter(isLoaded => isLoaded === true),
take(1),
).subscribe(() => {
this.monacoLoaderService.isMonacoLoaded$
.pipe(
filter(isLoaded => isLoaded),
take(1)
)
.subscribe(() => {
// This seems to be the issue during test runs - monaco is not defined
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({});
})
})
}
}
在我的组件文件中,monaco
正在从以下公开的接口中解析@materia-ui/ngx-monaco-editor
:
测试规范
describe('MyEditorComponent Test Suite:', () => {
let component: MyEditorComponent;
let fixture: ComponentFixture<MyEditorComponent>;
let tagsService: TagsService;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyEditorComponent],
providers: [ MonacoEditorLoaderService ],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyEditorComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('Should create component', () => {
expect(component).toBeTruthy();
});
});
在对我的组件进行单元测试时,我收到来自 Karma 的错误,告诉我未定义 monaco:
我正在努力让摩纳哥被 Karma 正确接收,至少我相信这是我的问题。
安装包时也安装@materia-ui/ngx-monaco-editor
了monaco-editor
,所以我也尝试将 monaco-editor 脚本添加到我的 angular.json 文件中我的测试项目配置部分的脚本属性中:
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"karmaConfig": "./karma.conf.js",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"stylePreprocessorOptions": {
"includePaths": [
"src/styles",
"node_modules"
]
},
"scripts": [
"node_modules/monaco-editor/esm/vs/editor/editor.main.js"
],
...
}
}
这没有什么区别,那么monaco
在我的测试套件中正确注册和提取的正确方法是什么?
更新
根据@Lucho 的建议,我在以下 Stackblitz 中复制了我的问题 - https://stackblitz.com/edit/materia-ngx-monaco-editor-ng9-zdpc2h?file=src/app/app.component.ts
请注意,与 Lucho 的方法不同,我没有将 MONACO_PATH 提供给 CDN 路径,而是在 angular.json 的assets
应用程序和测试项目配置部分的属性中添加一个条目,以确保 monaco-editor 库是提供
{
"glob": "**/*",
"input": "node_modules/monaco-editor",
"output": "assets/monaco-editor/"
}
所以,虽然我试图在 angular.json 文件的测试部分提供这个,但它似乎在实际测试运行中没有被以某种方式使用?