0

I just started writing unit tests for an existing Angular app using Jasmine, and around 50% of the time, I get the following error.The other 50% of the time, all tests pass without any issues.

The major problem I have with this error message is that Jasmine is not telling me where the issue

 An error was thrown in afterAll
  Uncaught TypeError: Cannot read property 'values' of undefined thrown
describe('deal without Angular testing support', () => {
    let service: DealService;
    let httpMock: HttpTestingController;

    beforeEach(async() => {

        TestBed.configureTestingModule({
            providers: [
                DealService
            ],
            imports: [HttpClientTestingModule],
            schemas: [ NO_ERRORS_SCHEMA ]
        });

        service = TestBed.get(DealService);
        httpMock = TestBed.get(HttpTestingController);
    });

    it('should use ValueService', () => {
        expect(service).toBeTruthy();
    });


    it('should return the json for deal http request', () => {
        service.getDealParameters({name:'sample name',value: "sample value"}).subscribe(data => {
            expect(data[0].dealParameterId).toBe(111);
            spyOn(service , "getDealParamDatafromSubject").and.callThrough();
            expect(service.getDealParamDatafromSubject).toHaveBeenCalled();
        });

        const req = httpMock.expectOne('https://localhost:44382/DPasdarameterSetup/GealParameters', 'call to api');
        expect(req.request.method).toBe('GET');

        req.flush([{
            dealParameterId: 111,
            actionComments: 'lorem',
            value: "sample value 1"

        },
        {
            dealParameterId: 222,
            actionComments: 'lorem',
            value: "sample value 2"

        }]);
        httpMock.verify(); 
    });
});

service code:
-----------------
export class DealParameterSetupService {
    scope: CScope[];
    primaryRestriction: CprimaryRestriction[];
    paramType: CparameterType[];
    secondaryRestriction: CSecondaryRestriction[];
    dealparams: CDealParameterSetup;
    dealParamData = new BehaviorSubject<any>('');
    dealData: Observable<any>;
    test = [];

    // Store the deserialize dealdata
    getDealParamDatafromSubject(dealData: any) {
        this.dealParamData.next(dealData);
    }

    constructor(private http: HttpClient) {
        this.dealData = this.dealParamData.asObservable();
    }

    // getDealParameters to get deal data
    getDealParameters(userDetailsObj): Observable<CDealParameterSetup[]> {
        return this.http.get<any>(environment.appBaseUrl + 'DPasdarameterSetup/GetDealParameters')
            .pipe(
                tap(
                    (data) => {
                        this.test = [];
                        data.map((dealData: CDealParameterSetup) => {
                            dealData.actionPerformedBy = userDetailsObj.userName;
                            this.test.push(new CDealParameterSetup().deserialize(dealData));
                            this.getDealParamDatafromSubject(this.test);
                            return this.test;
                        })
                    }
                )
            );
    }
});

How could I find where this error is coming from? Did anybody experience any inconsistency in Jasmine?

Sai Kumar
  • 31
  • 2
  • 12

1 Answers1

1

This may be the case of running the test randomly. In jasmine 3 running tests in random order is apparently now the default. By setting random to false may fix this.

  config.set({
    client: {
      jasmine: {
        random: false
      }
    }
  })
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
  • Thanks for help but its not working while placeing this code. now it is effecting to other test case failure instead of previous test case. – Sai Kumar Sep 17 '19 at 13:19
  • @SaiKumar, have you finally fixed that? I am facing the same issue. – dpatryas Apr 28 '20 at 11:59