Writing Tests in Angular

Angular CLI and test spec file

Not only does the Angular CLI install and set up Karma, as well as have a command to run all our tests, it also automatically generates a test spec file for us every time we use the CLI to generate a Component or service.

You may have seen this when we’ve used the ng generate command in earlier chapters, where we’ve created either a new Component or a service. The list of files being generated has always included a spec.ts file.

So, if we were to use the Angular CLI to generate a new component for use, we would use this command:

ng generate component my-comp

📝 Note: Click the terminal window and see what happens!

Get hands-on with 1400+ tech skills courses.