alpha2015tr
alpha2015tr

Reputation: 41

Jasmine-Karma UnitTest running single vs running all

When I run whole unit tests in a package, some of tests get fail although running individually those tests get success. I dont know what is affecting running in a set.

After executing "ng test --code-coverage" command, Jasmine-Karma creates the following output.

Component1 - Test Case 1 = Pass - Test Case 2 = Pass - Test Case 3 = Pass

Component2 - Test Case 1 = Pass / - Test Case 2 = Fail / - Test Case 3 = Pass /

For instance, if I run only Com2-Case2, it gets pass.

Thanks in advance.

Here is the typical unit test sample:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { FooterComponent } from './footer.component';

describe('FooterComponent', () => {

    let comp: FooterComponent;
    let fixt: ComponentFixture<FooterComponent>;

    beforeEach(async(() => {

        TestBed.configureTestingModule({

            declarations: [FooterComponent],

            imports: [
                TranslateModule.forRoot(),
            ],

        }).compileComponents();
    }));

    beforeEach(() => {
        fixt = TestBed.createComponent(FooterComponent);
        comp = fixt.componentInstance;
    });

    it('should create component === FooterComponent', () => {
        expect(comp).toBeTruthy();
    });

});

Upvotes: 3

Views: 1546

Answers (2)

Flavien Volken
Flavien Volken

Reputation: 21339

I had the same problem, it was a caching issue. Restarting Karma fixed the problem. For some reason, the "TestAll" tests were cached and fixing them separately did not change the result of the "TestAll" tests (while they did individually)

Upvotes: 0

alpha2015tr
alpha2015tr

Reputation: 41

I solved the issue by using "FakeSync" and "Tick".

I realized that if there is subscription inside of test case call, it is affecting the next test case. To overcome the issue, Tick was the solution.

it('should create TranslationTexts', fakeAsync(() => {
    comp.ngOnInit();
    tick();
    expect(JSON.stringify(comp.translationTexts).length).toBeGreaterThan(0);
}));

Tick helps to wait until async call is done. The following resource was very helpful.

https://codecraft.tv/courses/angular/unit-testing/asynchronous/

Thanks.

Upvotes: 1

Related Questions