Testing Angular
  • Overview
  • Overview
  • 1. Create a new Angular CLI application
  • 2. Run default tests
  • 3. Review the Angular CLI directory and testing tools set up
  • 4. Jasmine tests
  • 5. Add simple test
  • 6. Add simple failing test
  • 7. Add another test to check a property
  • 8. Test the Component logic using a mocked up Service
  • 9. Test the Component logic using SpyOn
  • 10. TestBed and Fixtures
Powered by GitBook
On this page

5. Add simple test

  • Delete the default tests made by the Angular CLI

**src/app/app.component.spec.ts

  • Add a describe block.

src/app/app.component.spec.ts
describe(`Component: App Component`, () => {

});
  • Add a it block.

src/app/app.component.spec.ts
describe(`Component: App Component`, () => {
  it('add 1+1 - PASS', () => {

  });
});
  • Add an expectation.

src/app/app.component.spec.ts
describe(`Component: App Component`, () => {
  it('add 1+1 - PASS', () => {
        expect(1 + 1).toEqual(2);
  });
});

Run the test with Karma test runner

  • In the terminal at the path of the project start the unit tests with the following command

ng test

Figure: Karma output in terminal

Previous4. Jasmine testsNext6. Add simple failing test

Last updated 6 years ago