18

I'm trying to add some tests to the node application I'm developing. I went through jest documentation for manual mocking and tried creating mocks folder as instructed. Please find the folder structure below.

app
 - firebase
  - fb.js
  - __mocks__
    - fb.js
    - firebase-admin.js
 - resolvers
    - mutation.js
__tests__
 - user.spec.js

As you can see, I have tried to mock two modules, fb.js (user module) and firebase-admin.js (node_modules module). firebase-admin.js mocking works without any problem. But user module mock is not even getting picked up by jest. The actual fb.js module is getting invoked all the time.

I have tried creating mocks directory for various user modules in my project but none of it is getting picked up. Is there any extra configuration I'm missing ??. currently I'm working around this problem by mocking firebase-admin node module only. But I want to mock the user module instead of firebase-admin module so that my firebase configurations are also mocked. Please let me know if any more information is needed.

__mocks__/fb.js

module.exports = {
   auth: jest.fn(() => "testing")
};

__mocks__/fb-admin.js

module.exports = {};

__tests__/user.spec.js

const request = require('supertest');
const server = require('../app').createHttpServer({});

const app = request(server);

describe('login resolvers', () => {
  test('should sign up user', async () => {
    const response = await app.post('/')
      .send({
        query: `mutation {
          signUp(idToken: "esd45sdfd...") {
            user {
              id
              locked
              revoked
            }
          }
        }
        `,
      })
      .set('Accept', 'application/json')
      .expect(200);
    console.log(response.text);
  });
});

app/resolvers/mutation.js

const admin = require('../firebase/fb');

/* other app code here */
Suneeth Lenin
  • 285
  • 1
  • 4
  • 10

2 Answers2

11

From the docs on Manual Mocks:

When we require that module in our tests, then explicitly calling jest.mock('./moduleName') is required.

If the module you are mocking is a Node module (e.g.: lodash), the mock should be placed in the __mocks__ directory adjacent to node_modules (unless you configured roots to point to a folder other than the project root) and will be automatically mocked. There's no need to explicitly call jest.mock('module_name').

doppelgreener
  • 4,809
  • 10
  • 46
  • 63
user487779
  • 550
  • 5
  • 12
  • Thanks. I'm not requiring the mocked module in my tests. It is used internally when the test code is being executed. So as per the documentation, explicit jest.mock is not required, right ? – Suneeth Lenin Apr 11 '19 at 15:33
  • No. You need to tell jest to mock the module when required by the sut – user487779 Apr 11 '19 at 16:31
  • 20
    This is an extremely confusing conversation to read. From the answer: "There's no need to explicitly call jest.mock('module_name')." -> Comment: "explicit jest.mock is not required, right?" -> Next comment: "No. You need to tell jest to mock the module". – Marisha Mar 31 '21 at 21:31
  • 4
    I am in the same situation where I'm not requiring the mocked module in my tests, but it's being used internally. Calling jest.mock('module_name') doesn't pull the module from `__mocks__`. But leaving it out doesn't do anything either. What are my other options? – Marisha Mar 31 '21 at 21:32
  • 3
    Mariana. 'There is no need to...' when a node module. The op is asking about a user module (not a node module) and thus required. – user487779 Apr 02 '21 at 07:26
  • 1
    Does "adjacent to node_modules" mean you place the __mocks__directory in "node_modules"'s parent folder? –  Dec 08 '21 at 23:28
  • @MontanaBurr, from the Jest docs IF you're mocking node modules the `__mocks__` folders goes in the same folder as `node_modules` e.g. `myApp/node_modules` and `myApp/__mocks__` – FranCarstens Dec 01 '22 at 21:04
6

I had to read the documentation very carefully. Especially the part about "unless you configured roots to point to a folder other than the project root". Double-check that you set up the __mocks__ folder in the source folder you specified for Jest. This is especially important in a monorepo or in general if you have configured jests root dir

Kenneth Lynne
  • 15,461
  • 12
  • 63
  • 79