【问题标题】:Angular 2 unit test parsing error using webpack使用 webpack 的 Angular 2 单元测试解析错误
【发布时间】:2016-12-03 02:31:49
【问题描述】:

当我使用 webpack 运行 Angular 2 单元测试时出现以下错误:

FAILED TESTS:
  Other Trims Tested
    ✖ should display other trims articles
      PhantomJS 2.1.1 (Linux 0.0.0)
    Error: Template parse errors:
    Unexpected closing tag "div" ("module.exports = "<div *ngFor=\"let trim of otherTrims\">\n  {{ trim.name }}\n[ERROR ->]</div>\n\n<!--<div class='test-name'>{{ otherTrims[0].name }}</div>-->\n";"): OtherTrimsTestedComponent@0:78 in /app/config/spec-bundle.js (line 52805)

我看到它正在测试 webpacks bundle-js 文件,但我不确定是否需要从 Angular 核心测试中进行另一个导入

我的 HTML 设置为:

<div *ngFor="let trim of otherTrims">
  {{ trim.name }}
</div>

和规范测试:

import {
  it,
  fdescribe,
  expect,
  inject,
  async,
  TestComponentBuilder
} from '@angular/core/testing';

import { OtherTrimsTestedComponent } from './other-trims-tested.component';

let mockData = [
  {
    'featured_image': 'http://test.url.com',
    'article_type': 'Test',
    'article_url': 'http://test.url.com',
    'name': 'Ford F-150 Raptor',
    'specs' : '4 Door AWD Pickup Truck, 150Bhp, 285 lb-ft, 8-sp Automatic',
    'msrp': '50,000'
  }
];

fdescribe('Other Trims Tested', () => {

  it('should have a selector', () => {
    let annotations = Reflect.getMetadata('annotations', OtherTrimsTestedComponent);
    expect(annotations[0].selector).toBe('other-trims-tested');
  });

  it('should display other trims articles', async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
    return tcb.createAsync(OtherTrimsTestedComponent).then(fixture => {

      // Get components
      let otherTrimsTestedComponent = fixture.componentInstance; // Get component instance
      let element = fixture.nativeElement; // Get test component elements

      otherTrimsTestedComponent.otherTrims = mockData;

      // Detect changes? (How?)
      fixture.detectChanges();

      // Test against data
      expect(element.querySelector('.test-name').innerText).toBe('Ford F-150 Raptor2');
    });
  })));
});

和组件:

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'other-trims-tested',
  template: require('./other-trims-tested.component.html')
})
export class OtherTrimsTestedComponent implements OnInit {

  @Input() otherTrims: any[];

  constructor() { }

  ngOnInit() { }

}

任何提示/文章都可以提供帮助。谢谢

【问题讨论】:

    标签: unit-testing typescript angular webpack angular2-testing


    【解决方案1】:

    您是否已将 html-loader 添加到您的 webpack 测试配置中?

    rules: [
     ...
        {
            test: /\.html$/,
            loader: 'raw-loader'
        }
    
    ...
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 2017-11-13
      • 2017-09-29
      • 2016-11-18
      • 2020-10-30
      • 2017-01-31
      • 2016-12-29
      • 1970-01-01
      相关资源
      最近更新 更多