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
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.
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.
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:
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
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: []
});
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