I am currently working on an Angular2 component that includes a tab control from @angular/material
.
During testing of my component (refer to the simplified code below), I encountered the following error:
Error: Error in ./MdTabHeader class MdTabHeader - inline template:0:0 caused by: No provider for ViewportRuler!
Error: No provider for ViewportRuler!
I attempted to resolve this issue by including ViewportRuler as a provider. However, when I did so (as shown in the commented out lines below), Karma returned:
Uncaught SyntaxError: Unexpected token import
at http://localhost:9876/context.html:10
After some research, it seems that the .ts file is being served to the browser instead of the compiled .js file. It's possible that I am referencing it incorrectly.
My main question is: how can I successfully compile my tests?
This is the code I have:
my.component.ts:
@Component({
selector: 'test',
template: require('./test.component.html')
})
export class TestComponent {
items: any[];
constructor() {
this.items = [{ title: 'test 1', description: 'description 1' }, { title: 'test 2', description: 'description 2' }];
}
}
my.component.html:
<md-tab-group>
<md-tab *ngFor="let link of items">
<template md-tab-label>
<h4>{{link.title}}</h4>
</template>
{{link.description}}
</md-tab>
</md-tab-group>
my.component.spec.ts:
import { TestBed } from '@angular/core/testing';
import { Component} from '@angular/core';
import { MaterialModule } from '@angular/material';
import { ViewportRuler} from '@angular/material/core/overlay/position/viewport-ruler'
import { TestComponent } from './test.component';
describe("TestComponent",
() => {
let fixture, component;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [MaterialModule],
declarations: [TestComponent],
providers: [
//{ provide: ViewportRuler }
]
});
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
});
it('true = true', () => {
expect(true).toBe(true);
});
});
I've tried to provide as much detail as I can, but I'm quite new to the Angular environment. Please let me know if there's anything else you need from me.
Thank you!