I am trying to incorporate Jest unit testing in a web application built with C8Y CLI v. 1016.0.324.
I have written an initial test on a component called the LiveDashboardComponent that uses two services: LiveDashBoardService and the FormBuilder. The LiveDashboardService, in turn, is injected with Cumulocity’s own MeasurementService.
I have setup my spec file as follows:
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormBuilder, FormGroup } from '@angular/forms';
import { NO_ERRORS_SCHEMA } from '@angular/compiler';
import { LiveDashboardComponent } from '../../app/live-dashboard/live-dashboard.component';
import { LiveDashboardService } from '../../app/live-dashboard/live-dashboard.service';
import { MeasurementService } from '@c8y/client';
describe('LiveDashboardComponent', () => {
let component: LiveDashboardComponent;
let fixture: ComponentFixture<LiveDashboardComponent>;
let mockDashboardService: LiveDashboardService;
let mockFormBuilder: FormBuilder;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
],
declarations: [
LiveDashboardComponent,
],
providers: [
LiveDashboardService,
FormBuilder,
MeasurementService
],
schemas: [
NO_ERRORS_SCHEMA
]
}).compileComponents();
})
beforeEach(() => {
fixture = TestBed.createComponent(LiveDashboardComponent);
component = fixture.componentInstance;
mockDashboardService = fixture.debugElement.injector.get(LiveDashboardService);
mockFormBuilder = fixture.debugElement.injector.get(FormBuilder);
})
afterEach(() => {
jest.clearAllMocks();
})
describe('ngOnInit', () => {
describe('getDays', () => {
it('should call the method getDays once', () => {
const spy1 = jest.spyOn(mockDashboardService, 'getDays').mockImplementation(() => null);
component.getDays();
expect(spy1).toBeCalledTimes(1);
})
it
})
})
})
The tests pass, but the compiler returns warnings signs because it cannot recognize the C8Y component <c8y-title>
as a regular Angular component.
> marketing-demo@1016.0.324 test:coverage
> jest --coverage
PASS ./app.module.spec.ts
PASS src/test/live-dashboard.component/live-dashboard.component.spec.ts
● Console
console.error
NG0304: 'c8y-title' is not a known element (used in the 'LiveDashboardComponent' component template):
1. If 'c8y-title' is an Angular component, then verify that it is a part of an @NgModule where this component is declared.
2. If 'c8y-title' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
at validateElementIsKnown (node_modules/@angular/core/fesm2020/core.mjs:10449:25)
at elementStartFirstCreatePass (node_modules/@angular/core/fesm2020/core.mjs:14471:9)
at ɵɵelementStart (node_modules/@angular/core/fesm2020/core.mjs:14509:9)
at LiveDashboardComponent_Template (ng:/LiveDashboardComponent.js:18:9)
at executeTemplate (node_modules/@angular/core/fesm2020/core.mjs:11835:9)
at renderView (node_modules/@angular/core/fesm2020/core.mjs:11635:13)
at renderComponent (node_modules/@angular/core/fesm2020/core.mjs:12840:5)
at renderChildComponents (node_modules/@angular/core/fesm2020/core.mjs:11494:9)
-|---------|----------|---------|---------|------------------- | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-|---------|----------|---------|---------|------------------- | 18.28 | 0 | 6.38 | 18.29 |
| 100 | 100 | 100 | 100 |
| 14.28 | 0 | 5.55 | 14.7 | 89-130,141-652
| 43.75 | 100 | 9.09 | 40 | 130-209
-|---------|----------|---------|---------|-------------------
Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 5.713 s
Ran all test suites.
Is there a way to solve this error or, if it’s not an error, is there a way I can hide these messages from the compiler?