Jest - 重置模块中的值并重新加载导入

时间:2021-01-27 10:59:27

标签: jestjs

我正在开玩笑地为使用来自不同模块的常量的模块编写测试。 我想为每个测试用例为其设置不同的值,但我似乎无法这样做。

测试文件:

import { Request, Response } from 'express';
const activityConsumer = require('../../src/utils/activity.consumer');

const mockRequest = {
  params: {
    activityArn: 'activityArn'
  }
} as Request;

const mockedJsonFunction = jest.fn();

const mockResponse: any = {
  json: jest.fn(),
  status: jest.fn().mockReturnValue({ json: mockedJsonFunction }),
} as Response;

let stopConsumerMock;

describe('consumer handler', () => {
  beforeAll(() => {
    stopConsumerMock = activityConsumer.stopConsumer = jest.fn().mockReturnValue(1);
  });
  beforeEach(() => {
    jest.resetModules();
  });
  afterEach(() => {
    stopConsumerMock.mockClear();
    mockResponse.json.mockClear();
  });
  describe('stopConsumingHandler', () => {
    it('Should return success true and not call stopConsumer when no consumer exists', () => {
      activityConsumer.consumer = undefined;

      const { stopConsumingHandler } = require ('../../src/handlers/consumer.handlers');
      stopConsumingHandler(mockRequest, mockResponse);

      expect(stopConsumerMock.mock.calls.length).toEqual(0);
      expect(mockResponse.json.mock.calls.length).toEqual(1);
      expect(mockResponse.json).toHaveBeenCalledWith({ success: true });
    });
    it('Should return success true and call stopConsumer when consumer exists', () => {
      activityConsumer.consumer = true;
      const { stopConsumingHandler } = require ('../../src/handlers/consumer.handlers');

      stopConsumingHandler(mockRequest, mockResponse);

      expect(stopConsumerMock.mock.calls.length).toEqual(1);
      expect(mockResponse.json.mock.calls.length).toEqual(1);
      expect(mockResponse.json).toHaveBeenCalledWith({ success: true });
    });
  });
});

我想替换 activityConsumer.consumer 的值,然后重新加载 consumer.handlers 模块,但是重新分配和重新加载似乎没有任何效果。

请告诉我如何正确编写此测试。

1 个答案:

答案 0 :(得分:0)

试试这个,用jest.mock修改activityConsumer的导入值

import { Request, Response } from 'express';
// const activityConsumer = require('../../src/utils/activity.consumer');

const mockRequest = {
  params: {
    activityArn: 'activityArn'
  }
} as Request;

const mockedJsonFunction = jest.fn();

const mockResponse: any = {
  json: jest.fn(),
  status: jest.fn().mockReturnValue({ json: mockedJsonFunction }),
} as Response;

let stopConsumerMock;

describe('consumer handler', () => {
  beforeAll(() => {
    // stopConsumerMock = activityConsumer.stopConsumer = jest.fn().mockReturnValue(1);
    stopConsumerMock = jest.fn().mockReturnValue(1);
  });
  beforeEach(() => {
    jest.resetModules(); // important line
  });
  afterEach(() => {
    stopConsumerMock.mockClear();
    mockResponse.json.mockClear();
  });
  describe('stopConsumingHandler', () => {
    it('Should return success true and not call stopConsumer when no consumer exists', () => {
      // activityConsumer.consumer = undefined;

      // mock by this way
      jest.mock('../../src/utils/activity.consumer', () => ({
        consumer: undefined,
        stopConsumer: stopConsumerMock,
      }));


      const { stopConsumingHandler } = require('../../src/handlers/consumer.handlers');
      stopConsumingHandler(mockRequest, mockResponse);

      expect(stopConsumerMock.mock.calls.length).toEqual(0);
      expect(mockResponse.json.mock.calls.length).toEqual(1);
      expect(mockResponse.json).toHaveBeenCalledWith({ success: true });
    });
    it('Should return success true and call stopConsumer when consumer exists', () => {
      // activityConsumer.consumer = true;

      // mock by this way
      jest.mock('../../src/utils/activity.consumer', () => ({
        consumer: true, // mock value for consumer
        stopConsumer: stopConsumerMock,
      }));

      const { stopConsumingHandler } = require('../../src/handlers/consumer.handlers');

      stopConsumingHandler(mockRequest, mockResponse);

      expect(stopConsumerMock.mock.calls.length).toEqual(1);
      expect(mockResponse.json.mock.calls.length).toEqual(1);
      expect(mockResponse.json).toHaveBeenCalledWith({ success: true });
    });
  });
});