joeeames / PSAngularUnitTestingCourse

files & status for my Pluralsight course on Unit Testing
246 stars 490 forks source link

ERROR in src/app/hero/hero.component.shallow.spec.ts(7,16): error TS2314: Generic type 'ComponentFixture<T>' requires 1 type argument(s). #6

Open RuudHermans opened 5 years ago

RuudHermans commented 5 years ago

I run into this error following the course at Pluralsight. All tests (11 so far) pass, but this error is thrown in the terminal (webstorm).

This concerns chapter 4, mocking child components.

My code:

import {ComponentFixture, TestBed} from '@angular/core/testing';
import {HeroesComponent} from './heroes.component';
import {HeroService} from '../hero.service';
import {Component, EventEmitter, Input, NO_ERRORS_SCHEMA, Output} from '@angular/core';
import {of} from 'rxjs/internal/observable/of';
import {Hero} from '../hero';
import {By} from '@angular/platform-browser';

describe('HeroesComponent', () => {
  let fixture: ComponentFixture<HeroesComponent>;
  let mockHeroService;
  let HEROES;

  @Component({
    selector: 'app-hero',
    template: '<div></div>',
  })
  class FakeHeroComponent {
    @Input() hero: Hero;
    // @Output() delete = new EventEmitter();
  }

    beforeEach(() => {
    HEROES = [
      {id: 1, name: 'SpiderDude', strength: 8},
      {id: 2, name: 'Wonderful Woman', strength: 24},
      {id: 3, name: 'SuperDude', strength: 55}
    ];
    mockHeroService = jasmine.createSpyObj(['getHeroes', 'addHero', 'deleteHero']);
    TestBed.configureTestingModule({
      declarations: [HeroesComponent, FakeHeroComponent],
      providers: [
        { provide: HeroService, useValue: mockHeroService }
      ]
      // schemas: [NO_ERRORS_SCHEMA]    // ignore child component
    });
    fixture = TestBed.createComponent(HeroesComponent);
  });

  it('should set heroes correctly from the service', () => {
    mockHeroService.getHeroes.and.returnValue(of(HEROES));
    fixture.detectChanges();
    expect(fixture.componentInstance.heroes.length).toBe(3);
  });

  it('should create one li for each hero', () => {
    mockHeroService.getHeroes.and.returnValue(of(HEROES));
    fixture.detectChanges();
    expect(fixture.debugElement.queryAll(By.css('li')).length).toBe(3);
  });
});
joeeames commented 5 years ago

not sure why you're getting that error. the first line after the describe you feed it a parameter. One thing to note, this is just a typescript error, so it won't affect execution (probably). So if things are working, you can switch the type of the fixture variable to "any" to avoid TS errors....