0

I am implementing tests in effects.

The test is giving the error:

[{"frame": 10, "notification": {"error": [TypeError: Cannot read
property 'getDowntimeOfProductShiftGraphic' of undefined], "hasValue":
false, "kind": "E", "value": undefined}}]

But getDowntimeOfProductShiftGraphic is a method in my service.

I followed the indications of this project as well as the talk of Jesse Sanders about Jest testing in ng-conf.

Below I have part of my test code.

export class TestActions extends Actions {
    constructor() {
        super(empty());
    }

    set stream(source: Observable<any>) {
        this.source = source;
    }
}

export function getActions() {
    return new TestActions();
}

describe('Auth Effects', () => {

    let actions: TestActions;
    let effects: StatisticsEffects;
    let serviceDownTime: IDownTimeRecordService;

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                StatisticsEffects,
                {
                    provide: Actions,
                    useFactory: getActions
                },
                {
                    provide: IDownTimeRecordService
                },
                {
                    provide: DowntimeRecordService,
                    useValue: { getDowntimeOfProductShiftGraphic: jest.fn() }
                },
                {
                    provide: ApiHttpService
                },
                {
                    provide: IMachineOperationService
                },
                {
                    provide: GlobalEnvironmentService
                },
            ]
        });

        actions = TestBed.get(Actions);
        effects = TestBed.get(StatisticsEffects);
        serviceDownTime = TestBed.get(DowntimeRecordService);
    });

    test('should create effect and services', () => {
      expect(effects).toBeTruthy();
      expect(serviceDownTime).toBeTruthy();
    });

    describe('some description', () => {
        it('should return something', () => {

            const request: DownTimeStatisticsChartRequestModel = {
                productId: 1,
                startDate: new Date()
            };

            const chartData: IShiftGraphicDto[] = [{
                Uptime: 100,
                Downtime: 200,
                Name: 'blabla'
            }];

            const action = new StatisticsActions.GetDownTimeStatisticsChart(request);

            const outcome = new StatisticsActions.GetDownTimeStatisticsChartSuccess(chartData);

            actions.stream = hot('-a', { a: action });
            const response = cold('-a|', { a: chartData });
            const expected = cold('-b', { b: outcome });

            expect(effects.getDownTimeStatisticsChart$).toBeObservable(expected);
        });
    });

Update

The solution for my problem was some configuration in webpack to recognize the tests. Thank you all that spent some time helping me.

Paulo Pereira
  • 211
  • 1
  • 8

1 Answers1

0

Try changing the expected as follows. Hope it helps:

const expected = cold('--b', { b: outcome });

While doing the assertion do it as

effects.getDownTimeStatisticsChart$.subscribe((actualAction) =>{ expect(actualAction).toBeObservable(expected) });

I hope that helps

RV.
  • 2,781
  • 3
  • 29
  • 46