Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular-cli tests fail after creating shared module

I've simply added a shared module with a SharedComponent in it and used that component in the main app component:

<acs-shared></acs-shared>

All that component does is display 'Hello, world!' using a property on the component:

<h1>Hello, {{name}}!</h1>

This all works fine when running the project with npm start, but now running npm test fails, so does $(npm bin)/karma start ./karma.conf.js. The first failure is that it can't create the component because it doesn't recognize the 'acs-shared' element. Is there something special that needs to be done to test components that use other components or modules?

Chrome 54.0.2840 (Windows 10 0.0.0) App: AngularCliStarter should create the app FAILED
    'acs-shared' is not a known element:

The project is available on github: https://github.com/JasonGoemaat/angular-cli-starter

like image 787
Jason Goemaat Avatar asked Oct 15 '16 04:10

Jason Goemaat


People also ask

Why do we need shared modules in angular?

If you try to add the component to multiple modules, Angular is going to throw you an error: It is in this situation that shared modules make sense. They are going to allow you to use components in multiples modules, share instances of services, and also should be the place to create the application’s common pipes and directives.

How do I run a test in angular CLI?

The Angular CLI downloads and installs everything you need to test an Angular application with the Jasmine test framework. The project you create with the CLI is immediately ready to test. Just run the ng test CLI command: The ng test command builds the application in watch mode , and launches the Karma test runner.

Why should I test my angular application?

Testing your Angular application helps you check that your app is working as you expect. Before writing tests for your Angular app, you should have a basic understanding of the following concepts:

How to create module with routing and specs in angular?

For example, to create module with routing and specs use the following command. $ ng g m guest -sp -r installing module create src/app/guest/guest-routing.module.ts create src/app/guest/guest.module.spec.ts create src/app/guest/guest.module.ts All generate module flags: PDF - Download angular-cli for free


1 Answers

You need to import the SharedModule into the TestBed configuration. What you're doing with the TestBed is like configuring an @NgModule from scratch for the test environment

TestBed.configureTestingModule({
  imports: [ SharedModule ],
  declarations: [
    AppComponent
  ],
  providers: []
});
like image 179
Paul Samsotha Avatar answered Oct 30 '22 17:10

Paul Samsotha