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') }));
Answer from jenkizenki on Stack Overflow
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Jest Testing, ...is not a function - JavaScript
May 12, 2022 - I’ve already successfully exported ... jest test written in the same way, but this one doesn’t want to work. The problem function in question is the generateCoordinates method inside the gameboardFactory function. When I run the tests, I get the type error that gameboardFactory.generateCoordinates is not a ...
Discussions

Why do I keep getting type error, "x" is not a function when using jest with create-react-app?

You need to do 'export default' in the sum function if you want to import the function without unpacking. Otherwise, you need to unpack when importing like 'import {sum} from...'

More on reddit.com
🌐 r/webdev
1
0
June 12, 2018
Writing tests: type error not a function
Hi, It is my first time writing tests, using jest. The error says that sortEvents is not a function even though it is: import React from "react"; import ReactDOM from "react-dom"; import App from "./components/App"; i… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
5
0
May 6, 2019
lightning web components - LWC JEST: TypeError: expect(...).toBeAccessible is not a function - Salesforce Stack Exchange
I am facing following error while running lwc test in newly created Salesforce DX project. I have installed JEST using sfdx force:lightning:lwc:test:setup command. This error I am facing only for More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com
January 10, 2023
Getting `TypeError: jest.fn is not a function`
The jest object is automatically in scope within every test file, so there's no need to import it explicitly. If you do want to import the jest object directly, you want to import the jest-mock module, not the jest-cli module, via: More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/webdev › why do i keep getting type error, "x" is not a function when using jest with create-react-app?
r/webdev on Reddit: Why do I keep getting type error, "x" is not a function when using jest with create-react-app?
June 12, 2018 -
// sum.js
export const sum = (a,b) => a+b

// sum.test.js
import sum from './sum';

it('sums numbers', () => {
  expect(sum(1, 2)).toEqual(3);
  expect(sum(2, 2)).toEqual(4);
});

I'm new to writing unit tests. I'm trying to write tests for my reducers and actions. However I can't seem to test a simple function that adds two numbers. I keep getting,

TypeError: (0 , _sum2.default) is not a function

What should I do??? Thanks.

🌐
Medium
medium.com › @pies052022 › jest-typeerror-is-not-a-function-solved-85226c79e1c7
Jest TypeError: is not a function [SOLVED] | by JOKEN VILLANUEVA | Medium
March 28, 2025 - The TypeError: is not a function is an error message that could appear while using the JavaScript testing framework Jest. Additionally, it is being used to test applications, libraries, and other JS…
🌐
Itsourcecode
itsourcecode.com › home › jest typeerror is not a function [solved]
Jest typeerror is not a function [SOLVED]
April 17, 2023 - In addition, Python is a high-level programming language that is used by most developers due to its flexibility. In conclusion, the “jest typeerror is not a function” occurs when we attempt to call a function on a value that is undefined.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Writing tests: type error not a function - JavaScript
May 6, 2019 - Hi, It is my first time writing tests, using jest. The error says that sortEvents is not a function even though it is: import React from "react"; import ReactDOM from "react-dom"; import App from "./components/App"; i…
Find elsewhere
Top answer
1 of 1
1

automock is a bad practice and should be preferably disabled. In case a module needs to be automatically mocked, this can be done explicitly with jest.mock. Manual mocks are preferable most times because they result in explicitly specified implementations that should be additionally provided to auto-mocks, too.

Jest auto-mocking is undocumented, it results in poorly understood magic that may not meet the expectations of a developer and may change without notice in next Jest versions. Class auto-mocking is briefly described in the guide. getAllData is instance method that is created inside a constructor. Jest auto-mocking relies on runtime contents of a module and checks static and prototype members, there's no way how it could process a constructor.

In order to be detected by Jest auto-mocking, getAllData should be prototype method, this is also suggested by common sense because there are no reasons for it to be an arrow:

export class TestService {
    getAllData(): Promise<any> {
        return { id: '1', name: 'test' }
    }
}

Auto-mocking doesn't result in a correct mock because the method is stubbed it doesn't return a promise. The problem could be determined at earlier point because auto-mocks require an implementation to be specified, this wasn't done in the OP:

const service = new TestService();
service.getAllData.mockResolvedValueOnce(...);
...

For prototype methods this can be done before an instance is available:

TestService.prototype.getAllData.mockResolvedValueOnce(...);
const service = new TestService();
...
🌐
Reddit
reddit.com › r/unittesting › jest test getting not a function error
r/unittesting on Reddit: Jest Test Getting Not a Function Error
November 29, 2023 - I have a very basic function that checks if a value exists in an array of values: const isValueInArray = (textToFind, arrayList) => { return arrayList.some(text => { return textToFind.includes(text) }) } If I try writing a unit test with Jest, I get the following error: arrayList.some is not a function ·
🌐
GitHub
github.com › kulshekhar › ts-jest › issues › 1873
Getting TypeError: foo_1.default is not a function · Issue #1873 · kulshekhar/ts-jest
August 11, 2020 - 🐛 Bug Report While refactoring a node project, I moved some files around (still in subfolders of src, adjusting the imports). After doing so, I get an error when running tests of Test suite failed to run TypeError: foo_1.default is not a...
Author   kulshekhar
🌐
codestudy
codestudy.net › blog › getting-typeerror-jest-fn-is-not-a-function
How to Fix 'TypeError: jest.fn is not a function' in Jest Unit Tests — codestudy.net
However, even seasoned developers encounter roadblocks, and one common frustration is the `TypeError: jest.fn is not a function` error. This error typically occurs when Jest’s mock function utility (`jest.fn()`) isn’t recognized, leaving tests broken and developers scratching their heads.
🌐
GitHub
github.com › redux-observable › redux-observable › issues › 286
TypeError is not a function in jest test · Issue #286 · redux-observable/redux-observable
July 29, 2017 - I took this pattern from https://stackoverflow.com/a/43599309 by @jayphelps This pattern works in usage but breaks in Jest tests. I get the error · ● Test suite failed to run TypeError: _Observable.Observable.merge is not a function · I was able to fix the issue using this pattern instead, but still curious if the above is a bug?
Author   redux-observable
🌐
Webdevtutor
webdevtutor.net › blog › typescript-jest-is-not-a-function
Troubleshooting 'Jest is not a function' Error in TypeScript
Ensure that the version of Jest you are using is compatible with your TypeScript setup. Sometimes, using an incompatible version of Jest can lead to the 'Jest is not a function' error.
🌐
DhiWise
dhiwise.com › post › fix-typeerror-expect-tobeinthedocument-is-not-a-function
Fix 'Typeerror Expect Tobeinthedocument Is Not A Function ...
April 12, 2024 - By importing @testing-library/jest-dom, you make additional matchers like .toBeInTheDocument() available in all test files. Now that we have our testing environment configured, let's address the typeerror expect tobeinthedocument is not a function.