trying to learn this TestBed testing TestBed in angular -2 with a simple example and hit my first blocker. google or SO search did not give any relevant example,
So, I have a very simple header component as shown below -
import { Component } from '@angular/core'; @Component({ selector: 'header', template: '' }) export class HeaderComponent{ public title: string; constructor(testparam: string){ this.title = 'test'; } }
and then its specification below -
import { TestBed } from '@angular/core/testing'; import { HeaderComponent } from './header.component'; describe('HeaderComponent Test', () => { let component: HeaderComponent; beforeEach(() => { TestBed.configureTestingModule({ declarations: [HeaderComponent] }); const fixture = TestBed.createComponent(HeaderComponent); component = fixture.componentInstance; }); it('should have the component defined', () => { expect(component).toBeDefined(); }); it('should initialize the title to test', () => { expect(component.title).toBe('test'); }); });
Run Karma Test - Error: No provider for String! in karma.entry.js Error: No provider for String! in karma.entry.js
karma.entry.js basically just sets up a test env configuration for TestBed and then goes through each test in my specs folder, below is my karma.entry.js
require('core-js/es6'); require('core-js/es7/reflect'); require('es6-shim'); require('reflect-metadata'); require('zone.js/dist/zone'); require('zone.js/dist/long-stack-trace-zone'); require('zone.js/dist/proxy'); require('zone.js/dist/sync-test'); require('zone.js/dist/jasmine-patch'); require('zone.js/dist/async-test'); require('zone.js/dist/fake-async-test'); require('rxjs/Rx'); const browserTesting = require('@angular/platform-browser-dynamic/testing'); const coreTesting = require('@angular/core/testing'); coreTesting.TestBed.initTestEnvironment( browserTesting.BrowserDynamicTestingModule, browserTesting.platformBrowserDynamicTesting() ); const context = require.context('../src', true, /\.spec\.ts$/); context.keys().forEach(context); Error.stackTraceLimit = Infinity; jasmine.DEFAULT_TIMEOUT_INTERVAL = 2000;
If I remove the parameter from the component class constructor, the tests will pass, so I think I am missing some preliminary configuration, which causes TestBed.createComponent(HeaderComponent) not correctly compile the component constructor with the string type parameter.
Any clue what I might be missing?
UPDATE:
if that helps anyone - based on @mrkosima's answer, my updated component class now looks lower and unit tests are all passing well now.
import { Component, OpaqueToken, Inject } from '@angular/core'; export let TITLE_TOKEN = new OpaqueToken('title token'); @Component({ selector: 'header', template: '', providers: [{ provide: TITLE_TOKEN, useValue: 'test' }] }) export class HeaderComponent{ public title: string; constructor(@Inject(TITLE_TOKEN) titleParam: string){ this.title = titleParam; } }