Generator-jhipster: Add more tests for Angular code

Created on 22 Oct 2017  ·  41Comments  ·  Source: jhipster/generator-jhipster

We need to add more typescript unit tests for the Angular code generated to make Sonar happy.
Another option is to find a way to make sonar count e2e tests

We could start with adding tests for the shared components and layout components

good first issue help wanted

Most helpful comment

Coverage is now 74% :smile: I guess with some more effort we can make it 80%

All 41 comments

Hi @deepu105, I would like to help with this. Are there other components already tested in the project that can inspire me to get started?

Yes just look at a generated application you should see some tests in src/test/javascript/spec directory which you can follow. I suggest to start adding tests for shared components and utilities to begin with

Yes that would be awesome! You can also have a look at doing tests on the entities: those are more complex to do, but as we generate a lot of them, the coverage will be much higher.
BTW, our official coverage is here: https://sonarcloud.io/dashboard?id=io.github.jhipster.sample%3Ajhipster-sample-application

In our project we are also missing some tests. The test generated for each entity was only the entity-detail.component.spec.ts which basically tests the typescript part of the component without HTML template.

What could be added:

  • Tests for list components (TS / HTML)
  • Tests for detail components (HTML)
  • Tests for services

We didn't solve this in a template by now but have added some tests manually. Maybe this excerpt of one of our service tests is useful:

        let service: EntityXxxService;

        beforeEach(() => {

            TestBed.configureTestingModule({
                providers: [
                    {provide: ConnectionBackend, useClass: MockBackend},
                    {provide: RequestOptions, useClass: BaseRequestOptions},
                    Http,
                    JhiDateUtils,
                    EntityXxxService,
                ],
            });

            service = TestBed.get(EntityXxxService);

            this.backend = TestBed.get(ConnectionBackend) as MockBackend;
            this.backend.connections.subscribe((connection: any) => {
                this.lastConnection = connection;
            });
        });

        it('should call correct URL', () => {
            service.find(123).subscribe(() => {});

            expect(this.lastConnection).toBeDefined();
            expect(this.lastConnection.request.url).toEqual('api/entityxxx/' + 123);
        });

        it('should return EntityXXX', () => {

            let entityXxx: EntityXXX;
            service.find(123).subscribe((_entityXxx: EntityXXX) => {
                entityXxx= _entityXxx;
            });

            this.lastConnection.mockRespond(new Response(new ResponseOptions({
                body: JSON.stringify({id: 123}),
            })));

            expect(entityXxx).toBeDefined();
            expect(entityXxx.id).toEqual(123);
        });

        it('should propagate not found response', () => {

            let error: any;
            service.find(123).subscribe(null, (_error: any) => {
                error = _error;
            });

            this.lastConnection.mockError(new Response(new ResponseOptions({
                status: 404,
            })));

            expect(error).toBeDefined();
            expect(error.status).toEqual(404);
        });

Hi guys, sorry for my late response.
I added tests for the login component. #6740

Thanks @clarenced this really helps us get our AAA rating back from sonar. Thanks a lot

You deserve a special :beer:

Oh that's awesome @clarenced !!!!!!!

You are welcome @deepu105 and @jdubois.

The coverage is now 72.6% nice. I guess we can reach 80% soon

I can continue until we reach it,

Yes please

Thanks & Regards,
Deepu

On Wed, Nov 29, 2017 at 3:37 PM, Clarence Dimitri CHARLES <
[email protected]> wrote:

I can continue until we reach it,


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/jhipster/generator-jhipster/issues/6581#issuecomment-347879029,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABDlF1kN8BKh2j6yUPml3de-p4ALal8wks5s7WwggaJpZM4QB_mR
.

Awesome!!!!! I see you are in Paris, will you come to our next meetup https://www.meetup.com/fr-FR/JHipster-User-Group/events/245305819/ ?

I can try but I work at Evry, I may arrive late

@clarenced that is far from Paris!! Just tell me if you come, even at the last minute, I'll keep a place for you.
(and if you look for a job closer to Paris, just ask!)

ok, I'll let you know

Nice work @clarenced but there is something I don't understand.
You add tests for login.component.ts here: login.component.spec.ts

But look at the Sonar Analyze for login.component.ts it seems to not beeing covered.

And the global coverage is still 67.7%, not 72.6% as mentionned by Deepu.

i'm adding test for entity listing. @pascalgrimaud I was looking here https://sonarcloud.io/dashboard?id=io.github.jhipster.sample%3Ajhipster-sample-application
image

And it does show some coverage for the login component
image

Probabaly im looking at wrong metrics, i'm not too familiar with sonar

@deepu105 : ok, I see. The 72.6% is the coverage on new code, not globally. The real value is still 67.7%

After launching yarn test, you can look into: target/test-results/coverage/app/shared/login/login.component.ts.html and the methods are not covered! It's so strange because the tests are coded!

Same to login.component.ts

Hi @pascalgrimaud, maybe it's a sonar configuration issue. As @deepu105, I am not familiar with sonar.

Not related to Sonar.
Locally, can you try:

  • launch yarn test
  • look into: target/test-results/coverage/app/shared/login/login.component.ts.html

OK I have found the reason, misplaced parenthesis. I will submit a new PR later

The new entity tests I added increased the overall coverage to 70.8%
https://sonarcloud.io/dashboard?id=io.github.jhipster.sample%3Ajhipster-sample-application
Let me see if I can add tests for entity dialog and delete componenets

@clarenced the login test cases are not at all executed due to incorrect closing braces. Always start with a failing test to make sure the test is being executed.

@clarenced I'm fixing it

OK

Coverage is now 74% :smile: I guess with some more effort we can make it 80%

@jhipster/developers btw if angular test doesnt interest you may be you can help to add some java tests as well where possible to improve the overall coverage especially on the entity side as it will yield better results

I would like to help in writing test coverage if this is still needed in the project. Could someone let me know please what should be added to the test .

@kedevked awesome! Have you read our Contributing guide?
Then have a look at our code coverage of the sample project -> this is what should be improved!

Yes, I did for the contribution guide.
As for the coverage I had a look at it. I am interested in writing tests mostly for the angular code. I saw in the thread that @clarenced has already started. I will try to add to the existing angular code @jdubois .

As @clarenced hasn't contributed for 3 weeks, I guess you can take whatever part you like: have a look at the entity sub-generator, this were you can have the biggest gain (as in the demo project we generate quite a lot of entities). Please do one PR per test -> having small PRs make it easy for us to merge, and that greatly reduce the chances somebody does the same as you. You can also comment here and tell on what you are working, so other people don't work on the same component.

Great @jdubois , it sounds good for me!

@kedevked awesome!! Looking forward 👍

Hello Team!

First (small) contribution to Angular test coverage: #6972
I'm starting with something small, there are few additional areas that I may try to cover,
then it would be great to tackle generated entity tests (we will see how it goes).

As for (coverage) reports, they seem to be not accurate (not all app files are listed in coverage report, only those that are included in some tests)? Can you confirm that?

@kamilcieslak thank you. Its much appreciated. Please do a comment here on what you are working on so that others dont duplicate same work
About coverage, not sure, need to check

@deepu105 my plan is as follows:

  • /app/shared/model is currently under review
  • /app/shared/alert -> I can submit this shortly (I should adjust this to reflect change to new HttpClient)
  • /app/shared/auth was next on my wish list
    by that time, I hope to get familiar enough with generator logic (when to generate stuff and when not to)
    to taclke
  • /app/entity/*

would you have any suggestions to change that order?

Regarding not all files being counted for coverage I've found this:
https://github.com/istanbuljs/babel-plugin-istanbul/issues/29
hope it helps

The order is fine thanks

On 11 Jan 2018 8:33 pm, "kamil" notifications@github.com wrote:

@deepu105 https://github.com/deepu105 my plan is as follows:

  • /app/shared/model is currently under review
  • /app/shared/alert -> I can submit this shortly (I should adjust this
    to reflect change to new HttpClient)
  • /app/shared/auth was next on my wish list
    by that time, I hope to get familiar enough with generator logic (when
    to generate stuff and when not to)
    to taclke
  • /app/entity/*

would you have any suggestions to change that order?

Regarding not all files being counted for coverage I've found this:
istanbuljs/babel-plugin-istanbul#29
https://github.com/istanbuljs/babel-plugin-istanbul/issues/29
hope it helps


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/jhipster/generator-jhipster/issues/6581#issuecomment-357036385,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABDlF9VMeBxIRVQ4f4IA6lVpWjB1GYjLks5tJmHrgaJpZM4QB_mR
.

short update:

  • /app/shared/model is awaiting merge, it will need update (to get rid of @angular/http in favor of @angular/commons/http)
  • /app/shared/alert -> I have local implementation (based on #6951 migration to HttpClient), I'll do PR as soon as migration will go into master (expected coverage change: 71.72% -> 75.43%)
    I'm still willing and eager to deliver tests according to my previous comment, but I think it would be less effort after new HttpClient will be in master

closing this as we pass the quality gate again. Thanks to @jdubois
If anyone wants to add more tests, you are always welcome as having 100% coverage would be super awesome

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pascalgrimaud picture pascalgrimaud  ·  3Comments

frantzynicolas picture frantzynicolas  ·  3Comments

edvjacek picture edvjacek  ·  3Comments

SudharakaP picture SudharakaP  ·  3Comments

marcelinobadin picture marcelinobadin  ·  3Comments