简体   繁体   中英

Jest 'TypeError: is not a function' in jest.mock

I'm writing a Jest mock, but I seem to have a problem when defining a mocked function outside of the mock itself.

I have a class:

myClass.js

class MyClass {
  constructor(name) {
    this.name = name;
  }

  methodOne(val) {
    return val + 1;
  }

  methodTwo() {
    return 2;
  }
}

export default MyClass;

And a file using it:

testSubject.js

import MyClass from './myClass';

const classInstance = new MyClass('Fido');

const testSubject = () => classInstance.methodOne(1) + classInstance.name;

export default testSubject;

And the test:

testSubject.test.js

import testSubject from './testSubject';

const mockFunction = jest.fn(() => 2)

jest.mock('./myClass', () => () => ({
    name: 'Name',
    methodOne: mockFunction,
    methodTwo: jest.fn(),
}))


describe('MyClass tests', () => {
    it('test one', () => {
        const result = testSubject()

        expect(result).toEqual('2Name')
    })
})

However, I get the following error:

TypeError: classInstance.methodOne is not a function

If I instead write:

...
methodOne: jest.fn(() => 2)

Then the test passes no problem.

Is there a way of defining this outside of the mock itself?

In my case, I had to mock a Node.js module. I'm using React and Redux in ES6, with Jest and Enzyme for unit tests.

In the file I'm using, and writing a test for, I'm importing the node modules as default:

import nodeModulePackage from 'nodeModulePackage';

So I needed to mock it as a default since I kept getting the error (0, _blah.default) is not a function. .

My solution was to do:

jest.mock('nodeModulePackage', () => jest.fn(() => {}));

In my case, I just needed to override the function and make it return an empty object.

If you need to call a function on that node module, you'll do the following:

jest.mock('nodeModulePackage', () => ({ doSomething: jest.fn(() => 'foo') }));

I figured this out. It is to do with hoisting, see: Jest mocking reference error

The reason it had worked in a previous test, where I had done it, was because the testSubject was itself a class. This meant that when the testSubject was instantiated, it was after the variable declaration in the test file, so the mock had access to use it.

So in the above case it was never going to work.

In my case, it was the importing, as described at Getting `TypeError: jest.fn is not a function` (posting here because my problem showed in jest.mock , so I looked here first).

The basic problem was that someone had put in

const jest = require('jest');

But that is wrong (should be jest-mock instead of jest). This had worked in Node.js 10 with the line as originally put. It did not work in Node.js 14. I'm guessing that they used different versions of Jest, perhaps indirectly (other packages needed updated for 14).

const jest = require('jest-mock');

Also, in a test file, it will probably already be done for you, so you don't need to require anything. The file will run properly without that line.

Linting may give an error with that line omitted, but that can be fixed with

/* global jest */

or if you're already doing that with expect ,

/* global expect, jest */

The linter seemed happy when that line appeared either after or before jest was first used. You may choose order based on readability rather than by functional requirement if your linter works the same way.

In my case it was from the module.exports .

Instead of writing

module.exports = {sum: sum};

Write

module.exports = sum;

Note : sum is a function that adds 2 numbers

In case anyone is still facing a similar issue, to resolve the failing tests I had to return a mocked function like so:

const tea = TeamMaker.makeTea();
tea(); // TypeError: tea is not a function


jest.mock('url/to/TeamMaker', () => ({ makeTea: jest.fn(() => jest.fn) })); // tests passed

Defining mockOne as an unassigned let and then initialising the variable inside the mocking function worked for me:

let mockFunction

jest.mock('./myClass', () => () => {
    mockFunction = jest.fn(() => 2)
    return {
        name: 'Name',
        methodOne: mockFunction,
        methodTwo: jest.fn(),
    }
}))

In my case, it was the way i was exporting the files that was the issue.

The file i was trying to mock was being exported like: export const fn = () => {};

The mocked file i wrote was being exported like:

const fn = () => {};
export default fn;

Once i made sure that the mocked file was also being exported like the file to be mocked, i didn't have this issue

following is the answer which worked for me

 import {testSubject} from '../testsubject';

 describe('sometest',(){

 it('some test scenario',()=>{
  
  testSubject.mockReturnValue(()=>jest.fn);

  })

 })

I am writing my version in case it helps someone.

I was facing error while mocking firebase-admin . I jest-mocked firebase-admin like below:

jest.mock('firebase-admin');

But I started getting following error:

firebase_admin_1.default.messaging is not a function

It was coming because in the app code, I had used firebase-admin like this:

await admin.messaging().send(message) 

(message is an instance of TokenMessage )

The problem was that I was not mocking the member variables and member methods. Here it was the member method messaging and a nested member method within messaging called send . (See that messaging method is called on admin and then send is called on the value of admin.messaging() ). All that was needed was to mock these like below:

jest.mock('firebase-admin', () => ({
  credential: {
    cert: jest.fn(),
  },
  initializeApp: jest.fn(),
  messaging: jest.fn().mockImplementation(() => {
    return {
      send: jest
        .fn()
        .mockReturnValue('projects/name_of_project/messages/message_id'),
    };
  }),
}));

(Note that I have also mocked other member variables/methods as per my original requirement. You would probably need these mocks as well)

For me, it was the jest config.

Because my project was initially in .js and was upgrade to .ts .

So the class I tested was in .ts but my jest.config.js was config for .js file.

See below my new config :

 module.exports = {
  ...
  collectCoverageFrom: ['src/**/*.{ts,js,jsx,mjs}'],
  ...
  testMatch: [
    '<rootDir>/src/**/__tests__/**/*.{ts,js,jsx,mjs}',
    '<rootDir>/src/**/?(*.)(spec|test).{ts,js,jsx,mjs}',
  ]
};

For the other Jest newbies out there, if you mock multiple functions from the same package or module like this:

jest.mock(
  'pathToModule',
  () => ({
    functionA: jest.fn(() => {
      return 'contentA';
    }),
  })
);

jest.mock(
  'pathToModule',
  () => ({
    functionB: jest.fn(() => {
      return 'contentB';
    }),
  })
);

The second mock will override the first, and you'll end up with functionA is not a function.

Just combine them if they're both coming from the same module.

jest.mock(
  'samePathToModule',
  () => ({
    functionA: jest.fn(() => {
      return 'contentA';
    }),
    functionB: jest.fn(() => {
      return 'contentB';
    }),
  })
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM