自从将我的 Angular 应用程序从版本 8 升级到版本 9 后,我在运行 Jest 单元测试时出现了一个新错误:
unsafe value used in a resource URL context (see http://g.co/ng/security#xss)
我正在测试的组件使用 DomSanitizer:
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
export class ExampleComponent implements OnInit {
@Input() path: string;
url: SafeResourceUrl;
constructor(
private sanitizer: DomSanitizer
) {}
ngOnInit(){
this.url = this.sanitizer.bypassSecurityTrustResourceUrl( this.path );
}
}
这个 url 用在 iframe 上:
<iframe [src]="url" />
我在 Angular 9 中使用 Jest,这发生在拍摄快照时。
我的测试(我试过嘲笑它而不是嘲笑它):
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ExampleComponent ],
providers: [
{
provide: DomSanitizer,
useValue: {
bypassSecurityTrustResourceUrl: () => ''
}
}
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ExampleComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should render', () => {
expect(fixture).toMatchSnapshot();
});
有谁知道我该如何解决这个问题?