0

I am trying to test an Effects method in Angular (NGRX) using Jest. The file imports the BigDecimal library. When I run the tests I get Cannot read property 'getPrettyValue' of undefined], undefined would refer to the usage of BigDecimal. I have tried a number of ways to get BigDecimal imported into the file but I still get undefined, even when I import the library into the spec file. The usage of BigDecimal is kept to the service file and only the method I am testing references it. Could it be something wrong with my testbed, or do I need to define the lib in a config file somewhere? Here is my Testbed configuration:

beforeEach(() => {
  TestBed.configureTestingModule({
    imports: [NxModule.forRoot(), HttpClientModule],
    providers: [
      ControllerEffects,
      DataPersistence,
      provideMockActions(() => actions),
      provideMockStore(),
      {
        provide: ControllerService,
        useValue: {
          getResponse: jest.fn()
        }
      }
    ],
    declarations: []
  });
  service = TestBed.inject(ControllerService);
  effects = TestBed.inject(ControllerEffects);
});

And the test that is failing due to this error:

it('should dispatch loadSuccess', () => {
  expect(service).toBeTruthy();
  actions = hot('-a-|', {
    a: ControllerActions.loadAll({ IdFilter: ['id'] })
  });
  const outcome = controllerActions.loadControllerSuccess({
    controller: pos
  });
  const response = cold('-a|', {
    a: {
      data: pos,
      status: { code: 'SUCCESS' }
    }
  });
  const expected = cold('--b|', { b: outcome });
  service.getResponse = jest.fn(() => response);
  expect(effects.loadAll$).toBeObservable(expected);
});

the reference to BigDecimal in the Effects file is returning the formatted value:

import BigDecimal from 'js-big-decimal';


method(input): string {
  return {
    valueString: BigDecimal.getPrettyValue(input.valueString, 3, ',')
  }
}

Any help is greatly appreciated, I've already spent a lot of time on this.

ConorJohn
  • 637
  • 2
  • 11
  • 24
  • 1
    Have you tried mocking the `BigDecimal` library in the test? https://stackoverflow.com/a/60669731/7365461 – AliF50 May 30 '22 at 13:07
  • 1
    @AliF50, that wasn't it exactly but it did set me on the correct path, thank you so much. – ConorJohn May 30 '22 at 16:29

0 Answers0