0

Env:

When I run a karma test an error occurs but It seems works well while server running.

SUMMARY:
✔ 2 tests completed
✖ 1 test failed

FAILED TESTS:
  Home Component
    ✖ should ...
      Chrome 55.0.2883 (Mac OS X 10.12.2)
    Error: Template parse errors:
    'my-searchbar' is not a known element:
    1. If 'my-searchbar' is an Angular component, then verify that it is part of this module.
    2. If 'my-searchbar' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("d="go_mypage" href="/mypage"><img src="/img/mypage-thin.png"></a>
    <div class="fold" id="wrapper">
      [ERROR ->]<my-searchbar></my-searchbar>
      <span>Home Works!</span>
    </div>"): HomeComponent@2:2

...

I have looked at several stack overflow questions or other websites related with this error but They were not very helpful.

app.module.ts

import { NgModule, ApplicationRef } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { MypageComponent } from './mypage/mypage.component';
import { HomeModule } from './home/home.module';
import { ApiService } from './shared';
import { routing } from './app.routing';

import { removeNgStyles, createNewHosts } from '@angularclass/hmr';

@NgModule({
  imports: [
    BrowserModule,
    HttpModule,
    FormsModule,
    routing,
    HomeModule
  ],
  declarations: [
    AppComponent,
    MypageComponent
  ],
  providers: [
    ApiService
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
  constructor(public appRef: ApplicationRef) {}
  hmrOnInit(store) {
    console.log('HMR store', store);
  }
  hmrOnDestroy(store) {
    let cmpLocation = this.appRef.components.map(cmp => cmp.location.nativeElement);
    // recreate elements
    store.disposeOldHosts = createNewHosts(cmpLocation);
    // remove styles
    removeNgStyles();
  }
  hmrAfterDestroy(store) {
    // display new elements
    store.disposeOldHosts();
    delete store.disposeOldHosts;
  }
}

app.component.html

<main>
  <router-outlet></router-outlet>
</main>

./home/home.component.html

<a class="route" id="go_mypage" href="/mypage"><img src="/img/mypage-thin.png"></a>
<div class="fold" id="wrapper">
  <my-searchbar></my-searchbar>
  <span>Home Works!</span>
</div>

./home/home.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';

import { HomeComponent } from './home.component';
import { SearchbarComponent } from './searchbar.component';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        RouterModule
    ],
    declarations: [
        HomeComponent,
        SearchbarComponent
    ],
    exports: [
        HomeComponent,
        SearchbarComponent
    ]
})

export class HomeModule { }

--Update--

./home/searchbar.component.ts

import { Component } from '@angular/core';

@Component({
selector: 'my-searchbar',
templateUrl: './searchbar.component.html',
styleUrls: ['./style/searchbar.component.scss']
})

export class SearchbarComponent {
    searchWord: string;
    searchTag: string[];

    onKey(event: any) {
        console.log(event.key);
    }
}

home.component.spec.ts

// This shows a different way of testing a component, check about for a simpler one
import { Component } from '@angular/core';

import { TestBed } from '@angular/core/testing';

import { HomeComponent } from './home.component';

describe('Home Component', () => {
  const html = '<my-home></my-home>';

  beforeEach(() => {
    TestBed.configureTestingModule({declarations: [HomeComponent, TestComponent]});
    TestBed.overrideComponent(TestComponent, { set: { template: html }});
  });

  it('should ...', () => {
    const fixture = TestBed.createComponent(TestComponent);
    fixture.detectChanges();
    expect(fixture.nativeElement.children[0].textContent).toContain('Home Works!');
  });

});

@Component({selector: 'my-test', template: ''})
class TestComponent { }
4
  • Can you add your test? Commented Dec 26, 2016 at 5:54
  • I just updated. Is that what you said? Commented Dec 26, 2016 at 6:49
  • 1
    I don't see SearchBarComponent here TestBed.configureTestingModule({declarations: [HomeComponent, TestComponent]}); Commented Dec 26, 2016 at 6:52
  • You made it! The error is gone and I got another one ; ) Thank you. I have to study more about jasmine. Commented Dec 26, 2016 at 7:12

1 Answer 1

1

The error/warning is explanatory, so pretty much what it says there:

You have created a custom element, <my-searchbar></my-searchbar> which is not a known Angular 2 element, and to suppress the warning add it to schemas in NgModule like so:

schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
Sign up to request clarification or add additional context in comments.

3 Comments

What is a difference between the "Angular Component" and the "Web Component"? I thought "my-seachbar" is an Angular Component. For the reason, I made home.module.ts and imported at app.module.ts
I'm not an expert in this. But to cut it very shortly and explain your situation: Your SearchbarComponent is the Angular component, where you have the @Component-notation before the selector: 'my-searchbar', which makes it a component. The <my-searchbar></my-searchbar> is a custom ELEMENT that you have put in your HomeComponent. Angular doesn't know what that is, unless you tell it that it is associated with the SearchbarComponent, which you have :) Someone smarter can explain better, but that is basically the difference :)
@BaroqueCode That's why it says 'my-searchbar' is not a known element in your error, because it isn't like e.g <p> or any other element that Angular recognizes by default and knows how to render the view :) Web Component can be a few things, but one of them is exactly what you have created, that is: "Web Component can be a custom element". Hopefully this was explanatory, and would you kindly accept the answer if it solved your problem :) (with the tick under the voting if you are unfamiliar)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.