简体   繁体   English

使用 jest 框架模拟基类

[英]Mocking a base class using jest framework

I have a base class which looks like the following.我有一个如下所示的基类。

class BaseClass {
  .....
  .....
  async request(options) {
    .......
    .......
  }
}

This is extended by another class like this.这是由另一个这样的类扩展的。

const BaseClass = require('@myorg/base-class').BaseClass;

class MyClass extends BaseClass {

      async method1() {
        .............
        request()
        .............
      }

      async method2() {
        .............
        request()
        .............
      }

}

Now I am using jest to test MyClass and it's 2 methods and to mock the BaseClass and trying to send a mocked response for it's request method.现在我正在使用 jest 来测试 MyClass,它有 2 种方法并模拟 BaseClass 并尝试为它的请求方法发送模拟响应。 Since I need to test both method1 and method2, I need to change the mock return after the first one.由于我需要同时测试method1和method2,所以我需要在第一个之后更改模拟返回。 I am doing like this.我正在这样做。

describe('MyClass', () => {
  afterEach(() => {
    jest.clearAllMocks();
    jest.resetAllMocks();
  });
  let MockClass = class BaseClass {
    request() {
      return {
        statusCode: 200,
        body: JSON.stringify({
          first_response: 'first_response'
        })
      };
    }
  };
  const mock = jest.mock('@myorg/base-class', () => {
    return {
      BaseClass: MockClass
    };
  });
  it('#method1 - test method one', async () => {
    const myClass = require('../src').myClass;
    const res = await myClass.method1();
    expect(res).toEqual('first_response');
  });

  it('#method2 - test method 2', async () => {
    mock.clearAllMocks();
    mock.resetAllMocks();
    MockClass = class BaseClass {
      request() {
        return {
          statusCode: 200,
          body: JSON.stringify({
            random_response: 'random_response'
          })
        };
      }
    };
    const myClass = require('../src').myClass;
    const res = await myClass.method2();
    expect(JSON.parse(res.body)).toEqual({
      random_response: 'random_response'
    });
    expect(res.statusCode).toEqual(200);
  });
});

The problem is once the mock is initialized, I am not able to reset and assign a different value as in the second test.问题是一旦模拟被初始化,我就无法像在第二个测试中那样重置和分配不同的值。 How can I achieve that ?我怎样才能做到这一点?

I had a similar problem a year ago.一年前我遇到了类似的问题。 I had 2 tests calling the same mocked method, but I wished to have 2 differents output.我有 2 个测试调用相同的模拟方法,但我希望有 2 个不同的输出。

Jest didn't provide any decent way to modify a mock once it is created, and I assume they didn't change that. Jest 没有提供任何体面的方法来修改创建后的模拟,我认为他们没有改变这一点。 (Can't link any source, sorry, it is a while ago). (无法链接任何来源,抱歉,这是前一段时间)。

I had the opportunity to discuss with some experimented and talented programmers, and the work-around was to make the tests in two different files , with 2 implementations for the mock.我有机会与一些经验丰富且才华横溢的程序员进行讨论,解决方法是在两个不同的文件中进行测试,并为模拟提供 2 个实现。

Simple, pretty clean if you encounter this problem few times, but not convenient if you have to do this often.如果您多次遇到此问题,则简单,非常干净,但如果您必须经常这样做,则不方便。

Please let me know if you have a better work-around or a fix!如果您有更好的解决方法或修复方法,请告诉我!

Good luck祝你好运

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

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