简体   繁体   English

jest.mock(中间值)……不是 function

[英]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:执行测试时,我从开玩笑中得到以下 output:

  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.我不知道为什么会这样,因为我认为 jest 会自动模拟构造函数和模块的所有方法。 Also, I am using typescript if that makes a difference here.另外,我正在使用 typescript 如果这在这里有所不同。

It turns out I didn't read the docs carefully enough.事实证明我没有足够仔细地阅读文档 It says that:它说:

Calling jest.mock('./sound-player') returns a useful "automatic mock" you can use to spy on calls to the class constructor and all of its methods.调用 jest.mock('./sound-player') 会返回一个有用的“自动模拟”,您可以使用它来监视对 class 构造函数及其所有方法的调用。 It replaces the ES6 class with a mock constructor, and replaces all of its methods with mock functions that always return undefined.它将 ES6 class 替换为模拟构造函数,并将其所有方法替换为始终返回未定义的模拟函数。 Method calls are saved in theAutomaticMock.mock.instances[index].methodName.mock.calls.方法调用保存在 AutomaticMock.mock.instances[index].methodName.mock.calls 中。

Please note that if you use arrow functions in your classes, they will not be part of the mock.请注意,如果您在类中使用箭头函数,它们将不会成为模拟的一部分。 The reason for that is that arrow functions are not present on the object's prototype, they are merely properties holding a reference to a function.原因是对象的原型上不存在箭头函数,它们只是持有对 function 的引用的属性。

So the issue in my case was that I used arrow functions in my class, and the mock was empty.所以我的问题是我在 class 中使用了箭头函数,而模拟是空的。

The correct version of the class looks like this: class 的正确版本如下所示:

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;
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM