I've tried to search every single similar issue here, unfortunately no solution worked for me.

I'm currently trying to mock named exports from a module, that looks like this:

module.ts

export function someFunctionOne() {// implementation}export const someFunctionTwo = function () {// implementation}export const someFunctionThree = () => {// implementation}export const someFunctionFour = () => {// implementation}

There are four exports here, and in my tests, I need to mock the whole module:

  • Two exports shall be mocked initially to some value, then re-mocked by some tests (but not all)
  • Two exports shall only be mocked initially and do not need to be re-mocked as I rely on the same mock for every tests

Tests look like this:

module.test.ts

import React from 'react'import { shallow } from 'enzyme'import Component from './component' // component I am testingimport { someFunctionOne, someFunctionTwo } from './module';(jest as any).mock('./module', () => ({someFunctionOne: jest.fn(),someFunctionTwo: jest.fn(),someFunctionThree: jest.fn(() => 10),someFunctionFour: jest.fn((s) => s),}));(someFunctionOne as any).mockReturnValue('some String');(someFunctionTwo as any).mockReturnValue(false)describe('Component', () => {beforeEach(() => {;(someFunctionOne as any).mockReset();(someFunctionTwo as any).mockReset()})it('renders', () => {;(someFunctionOne as any).mockImplementationOnce(jest.fn(() => 'some string'));(someFunctionTwo as any).mockImplementationOnce(jest.fn(() => false))const shallowRenderedModule = shallow(<Module><div>Children textContent here</div></Module>)expect(shallowRenderedModule).toMatchSnapshot()})// Then, many other tests...})

I also have jest and babel-jest configured as so:

jest.config.js

module.exports = {displayName: 'redacted-app',setupFilesAfterEnv: ['./jest/jest.setup'],preset: '../../jest.preset.js',transform: {'^.+\\.[tj]sx?$': ['babel-jest',{ cwd: __dirname, configFile: './babel-jest.config.json' },],'\\.(svg)$': './jest/svg.transform',},moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],}

babel-jest.config.json

{"presets": [["@babel/preset-env",{"targets": {"node": "current"}}],"@babel/preset-typescript","@babel/preset-react"]}

The problem is, no matter what method I use from Mock (mockImplementation, mockReturnValue, etc) I always end up with _moduleName.someFunctionOne.mockReturnValue is not a function:

 TypeError: _moduleName.someFunctionOne.mockReturnValue is not a function10 | someFunctionFour: jest.fn((s) => s),11 | }))> 12 | ;(someFunctionOne as any).mockReturnValue('some String')| ^13 | ;(someFunctionTwo as any).mockReturnValue(false)

console.logging it, it seems that indeed, the exports aren't actually mocked.

I'm at a loss here, what could it be?I've tried:

  • Using a dummy function inside the original jest.fn() e.g jest.fn(() => {})
  • Not using a jest mock function at all in the original mock e.g someFunctionOne: () => {}
  • Not using mockReturnValue in line 12 and mock it directly in jest.fn()
  • Chaining mockImplementationOnce to the initial module mock

But none of these worked.

2

Best Answer


I had similiar issues with mockimplementation is not a function and just realised I forgot the jest.setup.js file.

You probably already checked that, but have you looked in jest.setup.js instead of jest.config.js if you added the module there (jest.mock('...'));

That solved it for me as I wanted to mock a service. Was probably something I easily overlooked.

The error 'mockreturnvalue is not a function' typically occurs when attempting to call a function that does not exist. This error is commonly encountered in programming languages such as JavaScript, where functions are essential building blocks of code. When the code tries to execute a function with the name 'mockreturnvalue', but no such function is defined, the error is thrown.

To resolve this issue, it is important to ensure that the function 'mockreturnvalue' is properly defined and accessible within the code. Double-check the spelling and syntax of the function name to ensure it matches the intended function. If the function is supposed to be provided by an external library or module, make sure that it is correctly imported and initialized.

If the function 'mockreturnvalue' is expected to be defined within the code but is not found, it may be necessary to debug the code to identify any potential issues. Check for any typos, missing or incorrect import statements, or other code errors that could prevent the function from being recognized.

In some cases, the error may be caused by a misunderstanding or misuse of the function. Review the documentation or resources related to the 'mockreturnvalue' function to ensure it is being used correctly. Pay attention to any required parameters or specific usage instructions that could affect the function's behavior.

Overall, the 'mockreturnvalue is not a function' error can be resolved by verifying the existence and correct usage of the function within the code. Taking the time to carefully review the code and related resources can help in identifying and fixing the issue.