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:
app.component.spec.ts
are run and the results are shown on the browser just openedcomponent.spec.ts
are executed (i.e. the interruption of the first test process starts a second test process)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.
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.
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.
The Angular testing package includes two utilities called TestBed and async . TestBed is the main Angular utility package.
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.
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
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