2

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();
  });

これを修正する方法を知っている人はいますか?

4

1 に答える 1