Mock.mockImplementation 不是一个函数

gom*_*mes 11 javascript unit-testing typescript reactjs jestjs

我试图在这里搜索每个类似的问题,不幸的是没有解决方案适合我。

我目前正在尝试模拟模块中的命名导出,如下所示:

模块.ts

export function someFunctionOne() {
    // implementation
}

export const someFunctionTwo = function (
) {
    // implementation
}

export const someFunctionThree = () => {
    // implementation
}

export const someFunctionFour = () => {
    // implementation
}
Run Code Online (Sandbox Code Playgroud)

这里有四个导出,在我的测试中,我需要模拟整个模块:

  • 两个导出最初应被模拟到某个值,然后通过一些测试(但不是全部)重新模拟
  • 两个导出只能在最初被模拟,不需要重新模拟,因为我在每个测试中都依赖相同的模拟

测试如下所示:

模块.test.ts

import React from 'react'
import { shallow } from 'enzyme'
import Component from './component' // component I am testing
import { 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...
})
Run Code Online (Sandbox Code Playgroud)

我也有jestbabel-jest配置如下:

笑话配置.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'],
}

Run Code Online (Sandbox Code Playgroud)

babel-jest.config.json

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

Run Code Online (Sandbox Code Playgroud)

问题是,无论我使用什么方法MockmockImplementationmockReturnValue等),我总是会得到_moduleName.someFunctionOne.mockReturnValue is not a function

    TypeError: _moduleName.someFunctionOne.mockReturnValue is not a function

      10 |      someFunctionFour: jest.fn((s) => s),
      11 | }))
    > 12 | ;(someFunctionOne as any).mockReturnValue('some String')
         |                     ^
      13 | ;(someFunctionTwo as any).mockReturnValue(false)

Run Code Online (Sandbox Code Playgroud)

console.log仔细想想,似乎出口实际上并没有被嘲笑。

我在这里很茫然,这可能是什么?我试过了:

  • jest.fn()在原始示例中使用虚拟函数jest.fn(() => {})
  • 在原始模拟中根本不使用笑话模拟函数,例如someFunctionOne: () => {}
  • 不在第 12 行中使用mockReturnValue并直接在中模拟它jest.fn()
  • 链接mockImplementationOnce到初始模块模拟

但这些都不起作用。

小智 16

我遇到了类似的问题,mockimplementation is not a function,只是意识到我忘记了 jest.setup.js 文件。

您可能已经检查过了,但是如果您在其中添加了模块,您是否查看了 jest.setup.js 而不是 jest.config.js ( jest.mock('...'));

这为我解决了这个问题,因为我想模拟一项服务。可能是我很容易忽视的事情。