close

[Solved] TypeError: classInstance.methodOne is not a function in jest.mock

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error TypeError: classInstance.methodOne is not a function in jest.mock in jestJs. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How To Solve TypeError: classInstance.methodOne is not a function in jest.mock Error ?

  1. How To Solve TypeError: classInstance.methodOne is not a function in jest.mock Error?

    To Solve TypeError: classInstance.methodOne is not a function in jest.mock Error Just 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. 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.

  2. TypeError: classInstance.methodOne is not a function in jest.mock

    To Solve TypeError: classInstance.methodOne is not a function in jest.mock Error Just 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. 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.

Solution 1

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

Solution 2

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 to be 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 the order based on readability rather than by functional requirement if your linter works the same way.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment