Search code examples
typescriptunit-testingmockingts-jest

How do I use jest.mock within a describe or it block?


I'm using typescript and jest. I have two files, users.service.ts, which imports producer.ts. I want to mock a function in producer.ts. This works well

import { sendUserData } from './users.service';

const processDataSpy = jest.fn().mockImplementation(() => {
    throw new Error('failed');
  });

  jest.mock('../users/manager', () => ({
    sendEvent: async (arg1: string, arg2: string, arg3: any) =>
      processDataSpy(arg1, arg2, arg3),
  }));


describe('users.service', () => {

  it('invokes endpoint and returns proper data if unsuccessful', async () => {

    const result = await sendUserData(
      data
    );

    expect(result).toBe(false);

  });

However, I would like to mock different results in processDataSpy . I'm testing the case above throwing an error, but I'd like to test the case not throwing an error. How do I test multiple cases? Moving the "jest.mock" inside my "it" block breaks the test ...

  it('invokes endpoint and returns proper data if unsuccessful', async () => {
    
    jest.mock('../users/manager', () => ({
      sendEvent: async (arg1: string, arg2: string, arg3: any) =>
        processDataSpy(arg1, arg2, arg3),
    }));
    ...
    const result = await sendUserData(
      data
    );

    expect(result).toBe(false);

  });

I get an error indicating that the mock is no longer being used or started. How can I use "jest.mock" inside a "describe" or "it" block?


Solution

  • You can use processDataSpy.mockImplementation inside it or test block.

    // Optionally reset the mock before each test
    beforeEach(() => {
      processDataSpy.mockReset(); 
    });
    
    it('Another use case', async () => {
        
        processDataSpy.mockImplementation(() => {
          throw new Error('Another error');
        })
       
        const result = await sendUserData(
          data
        );
    
        expect(result).toBe(false);
    });