vendredi 11 décembre 2020

jest.mock (intermediate value).... is not a function

I have the following code I want to test:

export class MetricBuilder {
  metricDimensions: MetricDimensionsType;
  metricName: MetricName;
  metricValue: number;

  constructor() {
    this.metricDimensions = [];
    this.metricName = '';
  }

  public withReferenceId = (referenceId: string) => {
    this.addDimension(MetricDimensions.ReferenceId, referenceId);
    return this;
  }
};

My test file looks like this:

jest.mock('../../app/metrics/MetricBuilder');

import { MetricBuilder } from "../../app/metrics/MetricBuilder";

import Mock = jest.Mock;

let MetricBuilderMock: Mock;

describe('blah', () => {
  beforeEach(() => {
    MetricBuilderMock = MetricBuilder as Mock;
    MetricBuilderMock.mockReset();
  });

  it.only('blah', async () => {
    const a = new MetricBuilder().withReferenceId('sldkfj');
    expect(MetricBuilderMock.mock.instances[0].withReferenceId).toHaveBeenCalledTimes(1);
  });
});

I get the following output from jest when executing the test:

  TypeError: (intermediate value).withReferenceId is not a function

      14 | 
      15 |   it.only('blah', async () => {
    > 16 |     const a = new MetricBuilder().withReferenceId('sldkfj');
         |                                   ^
      17 |     expect(MetricBuilderMock.mock.instances[0].withReferenceId).toHaveBeenCalledTimes(1);
      18 |   });
      19 | });

I can't figure out why this is happening as I thought jest would automock the constructor and all the methods of the module.

Aucun commentaire:

Enregistrer un commentaire