0

So, I have two methods on a Node project:


export function methodA() {
  const response = await methodB();

  return response.length ? 'something' : 'else';
}

export function methodB() {
  const array = await getData(); // Access database and make API calls

  return array[];
}

methodA calls methodB and methodB makes stuff that I don't care right now for my unit testing purposes, so I want to mock methodB so it will return an empty array and won't try to make any database or API calls. The issue is that I can't actually mock methodB, as my test is still calling the actual function. Here's my test:

describe('method testing', () => {
  it('calls method', async () => {
    const response = await myModule.methodA();

    expect(response).toBe('else');
  });
});

That test is failing, because jest is still calling the actual methodB which is meant to fail, as it can't connect to the database or reach APIs, so, this is what I tried doing to mock methodB:

Spying on the method:

import * as myModule from '@/domains/methods';

jest.spyOn(myModule, 'methodB').mockImplementation(() => [] as any);

// describe('method testing', () => {...

Mocking the entire file except for the methodA:

jest.mock('@/domains/methods', () => {
  const originalModule = jest.requireActual('@/domains/methods')
  return {
    ...originalModule,
    methodB: jest.fn().mockReturnValue([])
  }
});

// describe('method testing', () => {...

I have also tried:

  • Mocking methodB inside each test and inside describe
  • Spying methodB inside each test and inside describe
  • Some variations of those examples I wrote above

I'm not entirely sure on what to do right now, so any light would be appreciated.

@Update: Altough the problem is similar, this is not a duplicate question and this (How to mock functions in the same module using Jest?) does not answer my question.

Lucas
  • 491
  • 6
  • 21
  • 1
    Does this answer your question? [How to mock functions in the same module using Jest?](https://stackoverflow.com/questions/45111198/how-to-mock-functions-in-the-same-module-using-jest) – jonrsharpe Jan 25 '23 at 21:41
  • (Don't: https://stackoverflow.com/a/70066090/3001761) – jonrsharpe Jan 25 '23 at 21:41
  • Unfortunately, it does not. – Lucas Jan 26 '23 at 23:19
  • Also, if you read my question and that answer very carefully, without much effort, you'll notice the "answer" there is of no help: `Trying to mock functions in the same module is like mocking parts of a class you're trying to test`. That is true for most cases, but it's (and I say that with respect), ridiculous of you to assume THAT is an absolute answer for every use case. Every application has a different context and reasons behing it's structure. Thanks, anyway. – Lucas Jan 26 '23 at 23:28
  • Well you haven't provided that context and there are _ten other answers_ on the dupe that explain and hack around exactly the same problem. In what you've shown you should probably mock `getData` instead. – jonrsharpe Jan 27 '23 at 07:35

0 Answers0