Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing an Angular Library, both the library components and the example app at the same time

I have created an Angular library using official guidelines and other posts (like this one).

The library is very simple, it contains just one component component.ts with its tests in component.spec.ts.

Together with the library I have also a sample app, where I show how to use component.ts. The sample app is represented by app.component.ts which has its own tests in app.component.spec.ts.

The scaffolding of my workspace has been created by Angular CLI (v 6.0.8). The structure of the workspace, at least for the files that I suspect are more relevant for my case, is the following

workspace
 - projects
   - library-name
     - src
       - lib
         - component.ts
         - component.spec.ts
       - test.ts
     - karma.config.ts
 - src
   - app
     - app.component.ts
     - app.component.spec.ts
   - karma.config.ts
 - angular.json

If I run ng test library-name only the tests of component.spec.ts are run.

If I run ng test without specifying the library name, because I want to test both the component and the example app, what happens is the following:

  • a browser is opened automatically by karma
  • the tests of app.component.spec.ts are run and the results are shown on the browser just opened
  • if I stop the process with ctrl+C a new process start and the tests of component.spec.ts are executed (i.e. the interruption of the first test process starts a second test process)
  • if I stop again the test with ctrl+C then eventually everything stops and I return to the command line

My question is if there is a way to run both the tests of app.component.spec.ts and component.spec.ts within the same process.

like image 324
Picci Avatar asked Aug 05 '18 08:08

Picci


People also ask

Which TestBed method is used to create an Angular component under test?

The TestBed. createComponent() method is used to create an instance of the AppComponent. The spec then uses expect and matcher functions to see if the component produces the expected behavior. As a result, the spec will either pass or fail.

What is the best testing framework for Angular?

When it comes to the Angular world, Jasmine is the recommended testing framework. Angular CLI comes by default with Jasmine and Karma as the test runner. It generates test files at component creation, collects unit tests, runs karma, and displays results on a web page.

Which is the main package for testing in Angular?

The Angular testing package includes two utilities called TestBed and async . TestBed is the main Angular utility package.

What are some of the different tests types you can write Angular?

You'll run across two types of tests when developing Angular applications: unit tests and E2E tests. You use unit tests to test the actual code, whereas you write E2E tests in a way that enables them to simulate user interactions. TypeScript is a language created by Microsoft that's a superset of JavaScript.


1 Answers

I know its an old post which is still unanswered but I looked into it and found the solution.

ng test example-ng6-lib-app --no-watch && ng test example-ng6-lib --no-watch

where example-ng6-lib is the library name as per the the attached article of the question.

Please give it a try

like image 139
Shashank Vivek Avatar answered Sep 29 '22 05:09

Shashank Vivek