Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
863 views
in Technique[技术] by (71.8m points)

jestjs - Jest - resetting value in module and reloading import

I'm writing a test in jest for a module which uses a constant from a different module. I want to set a different value for it for every test case, but I don't seem to be able to do so.

The test file:

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

I want to replace the value of activityConsumer.consumer and then reload the consumer.handlers module but the re-assignment and reload does not seem to have any effect.

Please advise on how can I write this test properly.

question from:https://stackoverflow.com/questions/65917738/jest-resetting-value-in-module-and-reloading-import

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this way, using jest.mock to modify import value of 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 });
    });
  });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...