Angular 在组件内部创建新实例时发生角度测试错误
我试图做一个简单的角度测试,我只是希望组件被创建。我使用这个库来处理模板中元素的大小调整,但是当我尝试创建对象的新实例时,Jest似乎不喜欢它 代码如下: 内联库.component.tsAngular 在组件内部创建新实例时发生角度测试错误,angular,typescript,jestjs,Angular,Typescript,Jestjs,我试图做一个简单的角度测试,我只是希望组件被创建。我使用这个库来处理模板中元素的大小调整,但是当我尝试创建对象的新实例时,Jest似乎不喜欢它 代码如下: 内联库.component.ts import { AfterViewInit, ChangeDetectorRef, Component, ElementRef, ViewChild, } from '@angular/core'; import ResizeObserver from 'resize-observer-
import {
AfterViewInit,
ChangeDetectorRef,
Component,
ElementRef,
ViewChild,
} from '@angular/core';
import ResizeObserver from 'resize-observer-polyfill';
@Component({
selector: 'app-inline-gallery',
templateUrl: './inline-gallery.component.html',
styleUrls: ['./inline-gallery.component.scss']
})
export class InlineGalleryComponent implements AfterViewInit {
@ViewChild('viewport') viewport: ElementRef<HTMLDivElement>;
constructor(private cd: ChangeDetectorRef) {}
ngAfterViewInit(): void {
const viewportObserver = new ResizeObserver(_ => {
console.log('test');
});
viewportObserver.observe(this.viewport.nativeElement);
}
}
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { InlineGalleryComponent } from './inline-gallery.component';
import { I18nModule } from '../../../i18n/i18n.module';
import {PreventDefaultDirective} from '../../directives/prevent-default/prevent-default.directive';
describe('GalleryComponent', () => {
let component: InlineGalleryComponent;
let fixture: ComponentFixture<InlineGalleryComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
InlineGalleryComponent,
PreventDefaultDirective
],
imports: [
I18nModule
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(InlineGalleryComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
在任何配置jest以获取typescript编译器选项的地方,都必须设置
“esModuleInterop:true”
。谢谢,该标志解决了这个问题,我将其添加到编译器选项
键内的tsconfig.json
文件中。在配置jest以获取typescript编译器选项的地方,您必须设置“esModuleInterop:true”
。谢谢,该标志解决了这个问题,我将其添加到tsconfig.json
文件中的编译器选项中。
FAIL src/app/shared/components/gallery/inline-gallery.component.spec.ts (5.678 s)
GalleryComponent
✕ should create (345 ms)
● GalleryComponent › should create
TypeError: resize_observer_polyfill_1.default is not a constructor
30 |
31 | ngAfterViewInit(): void {
> 32 | const viewportObserver = new ResizeObserver(_ => {
| ^
33 | console.log('test');
at InlineGalleryComponent.ngAfterViewInit (src/app/shared/components/gallery/inline-gallery.component.ts:32:30)
at callProviderLifecycles (../packages/core/src/view/provider.ts:570:14)