javascript - TypeError:ClassName.methodName 不是函数
问题描述
我正在嘲笑一个从节点模块导入的 ES6 类,如下所示:
// creating mocks
const mockMethodName = jest.fn()
jest.mock('../node_modules/my-module', () => {
const actualMyModule = jest.requireActual('../node_modules/my-module')
return {
...actualMyModule,
ClassName: jest.fn().mockImplementation(() => {
return {
methodName: mockMethodName
}
})
}
})
const { ClassName } = require('my-module')
// the test
it('does something successfully', () => {
// test logic that triggers ClassName.methodName()
expect(mockMethodName).toHaveBeenCalledTimes(1)
})
但我收到以下错误:
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with .catch().
The promise rejected with the reason
"TypeError: ClassName.methodName is not a function".] {
code: 'ERR_UNHANDLED_REJECTION'
}
我也尝试过像这样定义模拟方法:
const mockMethodName = jest.fn(() => {})
const mockMethodName = jest.fn(() => null)
const mockMethodName = jest.fn(() => true)
const mockMethodName = jest.fn(() => jest.fn)
但错误仍然存在。
我究竟做错了什么?如何正确模拟课堂?
PS:该ClassName.methodName()
方法在异步函数中调用,但ClassName.methodName()
不返回承诺
解决方案
推荐阅读
- swift - Swift - 为评论构建类似 instagram 的用户认证表视图
- django - 注册表单cleaned_data
- python - Docker 拉取镜像失败
- python - 在 Pyspark 中使用 python 多线程将数据并行写入表
- node.js - 将带有集群模块项目的node.js部署到heroku时绑定EADDRINUSE null错误
- pine-script - 在两行之间填充
- akka - 如何为oauth2 access_token做akka http请求
- java - 如何根据我在 excel 上的数据多次运行我的功能文件
- macos - CAReportingService 用完 RAM
- xml - XML类型定义不能是抽象的,xsd中没有非抽象类型