I am trying Angular 5 unit testing for the first time. While I already created the app then decided to run testing in it. But I am getting these errors:
AppComponent should create the app
AppComponent should have as title 'app'
AppComponent should render title in a h1 tag
GalleryComponent should create
UploadComponent should create
and error details like :
Failed: Template parse errors:
'app-upload' is not a known element:
1. If 'app-upload' is an Angular component, then verify that it is part of this module.
2. If 'app-upload' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. (" class="row">
<div class="col-md-3" style="padding:5%; box-sizing: border-box">
[ERROR ->]<app-upload></app-upload>
</div>
<div class="col-md-9">
"): ng:///DynamicTestModule/AppComponent.html@3:12
My package.json dev dependencies :
devDependencies": {
"@angular/compiler-cli": "^6.0.2",
"@angular-devkit/build-angular": "~0.6.3",
"typescript": "~2.7.2",
"@angular/cli": "~6.0.3",
"@angular/language-service": "^6.0.2",
"@types/jasmine": "2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~1.4.2",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1"
}
Test file app.component.spec.ts
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
}).compileComponents();
}));
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it(`should have as title 'app'`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('app');
}));
it('should render title in a h1 tag', async(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('Welcome to Typito-photo-app!');
}));
});
I couldn't figure out how to solve these. I haven't made any changes in spec files nor did I wrote any test cases. Shouldn't all these be running as expected according to what is described in angular documentation?
The component that you want to test has one or more child components. A good practice is to ignore these component and test these separate.
A way to archieve this is to tell your angular Testbed to skip these during the component building using NO_ERRORS_SCHEMA inside your Testbed.
import { NO_ERRORS_SCHEMA } from '@angular/core';
Then your Testbed should look like this:
TestBed.configureTestingModule({
declarations: [
AppComponent
],
schemas: [
NO_ERRORS_SCHEMA
]
}).compileComponents();
And it should ignore all custom elements(tags) that appears in your component.html.
Another way is to mock your child components. e.g like this
You should verify component to be available:
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
});
it('should be created', () => {
fixture.detectChanges();
expect(component)
.toBeTruthy();
});
Also you should inject any dependencies in the test Module while writing tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With